身份验证在Django中始终返回None

时间:2019-05-07 18:18:26

标签: django django-models django-forms django-authentication

验证始终返回无,并且我找不到适合我的答案

我认为问题出在我的模型或表格中

型号:

    class Employee(models.Model): 
    nameuser = models.CharField(max_length=100)
    passworduser = models.CharField(max_length=6) 
    class Meta:  
        db_table = "employee"  

在型号代码中,passworduser不应为密码类型吗?

表格:

    class EmployeeForm(forms.ModelForm):
    class Meta:  
        model = Employee  
        fields = "__all__"  

我的观点:

 from django.contrib.auth import authenticate, login as auth_login
 
 def login(request):
    form = EmployeeForm(request.POST)
    if request.method == 'POST' and form.is_valid():
            username =  form.cleaned_data['nameuser']
            password =  form.cleaned_data['passworduser']
            user = authenticate(username=username, password=password)
            if user is not None:
                auth_login(request, user)
                return redirect("/index")
                
            else:
                print (username) #Return the input correctly
                print(password) #Return the input correctly
                print(user) #Return None
                print(form)
    else:
        form = EmployeeForm()
    return render(request, 'htmlfiles/login.html')

我已经添加了模块:

AUTHENTICATION_BACKENDS=('django.contrib.auth.backends.ModelBackend',)

仍然无法正常工作,谢谢!

0 个答案:

没有答案