如何在 Django 中使用自定义模型对用户进行身份验证?

How to authenticate user using custom model in Django?

我正在做一个 Django 项目。该项目有首页、登录、注册三个页面。主页的导航栏有主页、登录和注册三个按钮。我创建了一个我不想管理模型的自定义模型。我已经完成了登录部分,但现在我想要的是在登录后主页导航登录和注册按钮消失,并且出现一个新的 button/text Hello user 或无论谁登录的名字。 有什么办法吗?我知道我们可以用管理员用户模型来做,但我不知道如何用自定义模型来做?

models.py

class  user_detail(models.Model):
    email = models.EmailField(("email"), max_length=254)
    first_name = models.CharField( ("first name"),max_length=50)
    last_name = models.CharField(("last name"), max_length=50)
    address = models.CharField(("address"), max_length=50)
    phone_number = models.IntegerField(("phone number"))
    password = models.CharField(("password"), max_length=50)

views.py

from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from .models import user_detail

class loginView(TemplateView):
    template_name = 'accounts/login.html'

    def post(self, request):
        if request.method == 'POST':
            print(request)
            html_email = request.POST['email']
            html_password = request.POST['password']

        user = user_detail.objects.values_list('email',flat = True)
        if html_email in list(user):
            user_password = user_detail.objects.filter(email = html_email).values_list('password',flat = True)[0]
            print(user_password)

            if user_password == html_password :
                return redirect('/home/')
        else:
            print(" incorrect credentials ")

        return render(request, 'accounts/login.html')

编辑:我需要的是每当有人登录时 login/register 按钮消失并欢迎,+ first_name .

拥有自定义用户模型的最佳方式是使用 AbstractUser。 你应该这样做:

Class User(AbstractUser):
    # add your custom field here
    birth_date=models.IntegerField()