Django问题总结

2018-04-05  本文已影响215人  冷煖自知

安装django-users2

INSTALLED_APPS = [
   ...
    'users',
   ...
]

AUTH_USER_MODEL = 'users.User'
python manage.py makemigrations
python manage.py migrate

tip:

import sys
reload(sys)
sys.setdefaultencoding('utf8')

如果用python manager.py启动,则在 manager.py头部添加
如果用uwsgi启动,需要在uwsgi.py头部添加

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

需要在Django的init文件中添加

import pymysql
pymysql.install_as_MySQLdb()
python manage.py collectstatic

如果报错django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
需要在setting.py中添加

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

django自带User进行验证

from django.contrib.auth import authenticate
user = authenticate(username=username, password=password)  # 验证用户名和密码,返回用户对象
from django.contrib.auth.hashers import make_password, check_password
#创建django密码, 第二个参数为None是每次产生的密码都不用,第三个参数为算法, 后面两个参数可以忽略
dj_ps = make_password(password.decode('utf-8'), None, 'pbkdf2_sha256')  
# check_password 返回值为一个Bool类型,验证密码的正确与否
ps_bool = check_password(ps, dj_ps)

听雨阁

上一篇下一篇

猜你喜欢

热点阅读