登录电子邮件代码错误

时间:2017-09-04 18:11:33

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

为什么我无法设法让我的登录工作..

views.py

def candidate_login(request):
    if request.method == 'POST':
       email = request.POST.get('email')
       password = request.POST.get('password')

       user = authenticate(email=email,password=password)

       if user:
          if user.is_active & user.check_password(password):
             login(request,user)
             return HttpResponseRedirect(reverse('index'))
          else:
             HttpResponse("Account not active, please contact Admin")
       else:
          print("Someone tried to login and failed")
          return HttpResponse("Invalid login detailed supplied!")
     else:
        return render(request,'candidate_login.html',{})

当我尝试登录时,收到error消息:提供的登录详细信息无效!

你能帮助我让它发挥作用吗? 非常感谢你 圣拉斐尔

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.


class MyUser(AbstractUser):
   is_hr = models.BooleanField(default=False)
   is_candidate = models.BooleanField(default=False)
   is_employee = models.BooleanField(default=False)
   company = models.CharField(max_length=100, default='')

1 个答案:

答案 0 :(得分:1)

authenticate需要用户名&密码验证。您可以直接过滤用户模型以获取与给定电子邮件链接的用户,然后使用check_password函数验证密码。

def candidate_login(request):
    if request.method == 'POST':
       email = request.POST.get('email')
       password = request.POST.get('password')

       user = User.objects.filter(email=email)

       if user:
          user=user[0]
          if user.is_active & user.check_password(password):
             login(request,user)
             return HttpResponseRedirect(reverse('index'))
          else:
             HttpResponse("Account not active, please contact Admin")
       else:
          print("Someone tried to login and failed")
          return HttpResponse("Invalid login detailed supplied!")
     else:
        return render(request,'candidate_login.html',{})