标题Python 用户验证authenticate()返回值为none的临时解决办法

views.py

from django.shortcuts import render
from django.contrib.auth import authenticate,login


# Create your views here.
def User_login(request):
    if request.method == "GET":
        return render(request, 'login.html')
    else:
        usname =request.POST.get('username')
        psd = request.POST.get('password')
        user = authenticate(username=usname,password=psd)
        if user:
            login(request,user)
            return render(request,'index.html')
        else:
            return render(request,'login.html',{})

网上找了很多关于authenticate()返回值为什么为none的帖子,几乎都没有解决办法,最后想到是不是Django对Mysql数据库的支持不是很好,所以改用了django默认的sqlite3数据库
在setting.py中把数据库文件改回来

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

运行manage.py
makemigrations
完成后:
migrate
再运行debug模式,request终于不再是none了

你可能感兴趣的:(python教程)