Django Authenticate也总是返回None并带有正确的凭据

时间:2019-12-26 15:41:51

标签: python django authentication

我正在为我的项目创建一个登录和注册页面,并且正在使用Django MySQL数据库进行登录并创建用户,因此注册已正确完成,但是登录功能对我而言无效。

PS:is_active设置为True,但仍然无法正常工作

使用所有正确的信息,shows无。我已经给出了文件和代码的所有必要部分

我尝试了一切,但似乎无济于事,也尝试了StackOverflow上列出的某些解决方案,但仍然无济于事。

from django.shortcuts import render, redirect
from .models import Description
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login

# Create your views here.
def index(request):
    d = Description.objects.all()
    return render(request,'index.html',{'des':d})
def signup(request):
    if request.method == 'POST':
        first_name = request.POST['first']
        last_name = request.POST['last']
        user_name = request.POST['user']
        email = request.POST['email']
        pass1 = request.POST['password1']
        pass2 = request.POST['password2']
        if pass1 == pass2:
            if User.objects.filter(email=email).exists():
                print("Email taken")
            elif User.objects.filter(username=user_name).exists():
                print("Username taken")
            else:        
                user = User.objects.create_user(username = user_name, first_name=first_name,last_name=last_name, password=pass1, email=email)
                user.save()
                print("User created")
        else:
            print("Password Not Matching")  
        return redirect('/')      
    else:
        return render(request,'signup.html')   
def login(request):
    if request.method == 'POST':
        email = request.POST['email']         
        password = request.POST['password']
        print(email,password)
        user = authenticate(request, email=email, password=password)
        print(user)
        if user is not None:
            login(request, user)
            print("Logged in")
        else:
            print("not logged in")    
        return redirect('/')
    else:
        return HttpResponse("Invalid")        

3 个答案:

答案 0 :(得分:0)

问题是您的注册方法。您不应将password赋予init函数。您应该使用用户的set_password方法。

在您的sign_up视图中,删除创建用户对象所在行的password=pass1。然后,在保存用户之前,添加user.set_password(pass1)

希望这会有所帮助!

答案 1 :(得分:0)

问题是django的默认身份验证机制不允许您使用DIR/lib64进行身份验证,而只能使用email进行用户身份验证。 无论哪种方式,您都可以使用其电子邮件从username获取用户的用户名并对用户进行身份验证。

user model

也不要直接存储密码,始终使用Django的用户标准 def login(request): if request.method == 'POST': email = request.POST['email'] password = request.POST['password'] username = '' try: username = User.objects.get(email=email).username except User.DoesNotExist: return HttpResponse("Invalid") user = authenticate(request, username=username, password=password) if user is not None: login(request, user) print("Logged in") else: print("not logged in") return redirect('/') else: return HttpResponse("Invalid") 功能

set_password()

答案 2 :(得分:0)

Django <resultMap type='Make' id='resultMap'> <id property='makeId' column='make_id' /> <result property='makeName' column='make_name' /> <collection property="models" ofType="Model" javaType="list"> <id property='modelId' column='model_id' /> <result property='modelName' column='model_name' /> </collection> </resultMap> 方法仅允许使用用户名进行身份验证。 您可以使用用户的电子邮件从用户模型中获取用户的用户名

authenticate

替换此username = User.objects.get(email=email).username password = request.POST.get('password')

与此user = authenticate(request, email=email, password=password)