Django 无法验证用户名和密码,尽管数据存在于数据库并且输入的数据已正确检查

问题描述 投票:0回答:1

view.py

def loginverify(request):
    print("-----------login verification------------")
    if request.method == "POST":
        cemail = request.POST.get("cemail")
        cpassword = request.POST.get('cpassword')
        print(cemail, cpassword) (able to print the data sent )

        User = authenticate(request, email=cemail, password=cpassword)
        print(User)-->(this shows none)
        if User is not None:
            login(request, User)
            print("no problem")
            messages.success(request, 'Login successful')
            return redirect('dashboard')  # Redirect to the user's dashboard or landing page
        else:
            messages.error(request, 'Invalid email or password')
            print("problem occurred")

            template = 'polls/login.html'
            return render(request, template)  # Redirect to login page

    template = 'polls/CustLandingpage.html'
    return render(request, template)

此功能接收到数据,但身份验证不起作用

模型.py

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

# Create your models here.
class Customer(models.Model):
  name = models.CharField(max_length=20)
  Email = models.CharField(max_length=50)
  password = models.CharField(max_length=20)
    

        
  USERNAME_FIELD = 'Email'
  REQUIRED_FIELDS = ['name']

使用电子邮件和密码进行身份验证。我尝试了大部分方法,但都不起作用。

python django django-models django-views
1个回答
0
投票

我相信您的客户模型不会扩展身份验证框架提供的任何模型。

因此,

class Customer(AbstractUser):

在 settings.py 中设置客户

使用电子邮件字段和用户名字段:

class Customer(AbstractUser):
# If you don't want to use the built-in username field, 
set it to None or use the 'email' field
username = None
email = models.EmailField('email address', unique=True)  

USERNAME_FIELD = 'email'  # Use email 
REQUIRED_FIELDS = []

使用 makemigrations 并迁移

让我知道你的进展如何,祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.