python - 验证登录 Django 的自定义用户模型
问题描述
我在我的 Django 项目中创建了我的自定义用户模型。我的用户注册工作正常。但是,我的登录不是。
这是我的自定义用户模型:
from django.db import models
import datetime
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
class NewUser(AbstractBaseUser, PermissionsMixin):
username = models.CharField(_('username'), max_length=150, primary_key = True)
firstName = models.CharField(max_length=150)
lastName = models.CharField(max_length=150)
nombreArtistico = models.CharField(max_length=150, default=None, null=True, unique=True)
#Validacion create_superuser
last_login = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
#Info de la suscripcion_form
activa = models.BooleanField(default=False)
fecha_creacion = models.DateField(default=None, null=True)
fecha_actualizacion = models.DateField(default=datetime.date.today)
disponibilidad_de_reproducciones = models.IntegerField(default=3)
# objects = CustomAccountManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['firstName', 'lastName']
def __str__(self):
return self.username
这是我的身份验证登录表单:
from django.forms import ModelForm
from django import forms
from usuarios.models import NewUser
from django.contrib.auth.forms import UserCreationForm
from django.conf import settings
from django.contrib.auth import get_user_model
User = get_user_model()
class Register(forms.Form):
username = forms.CharField(label="username", max_length=150)
firstName = forms.CharField(label="First Name", max_length=150)
lastName = forms.CharField(label="Last Name", max_length=150)
password = forms.CharField(label="Password", max_length=150, widget=forms.PasswordInput)
class AuthenticationForm(forms.Form):
username = forms.CharField(label="username", max_length=150)
password = forms.CharField(label="Password", max_length=150, widget=forms.PasswordInput)
这就是我在我的登录视图中尝试的。
def login_view(response):
if response.method == "POST":
print(response.POST['username'])
form = AuthenticationForm(response.POST)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
userauth = authenticate(username=username, password=password)
if userauth is not None:
login(response, user1)
return HttpResponseRedirect('/profile/')
#return redirect()
else:
form = AuthenticationForm()
return render(response, 'login/login.html', {"form2":AuthenticationForm()})
这是我得到的错误。我的猜测是我正在使用的函数 authenticate() 与我的自定义用户模型无关。
重要的是我重新定义了我的用户模型,如下所示:AUTH_USER_MODEL = 'usuarios.NewUser'
解决方案
您已经创建了一个自定义用户模型,但您还没有像在 django 中处理所有其他模型那样创建它的管理器。尝试从官方文档中的完整示例构建。
重要的是要注意
如果您只需要扩展默认用户模型,请考虑从AbstractUser继承并从 AbstractBaseUser继承,如果您想要进一步自定义。
推荐阅读
- reactjs - 无法将数据发送到 firebase、React、ES6、Redux
- html - 如何在 Html 中创建特定表?
- scheme - SICP 1.25 解释器问题
- javascript - 如何根据产品点击在不同产品的特定 div 同一页面上显示产品描述?
- php - Stripe Exp_Month 上的前导零
- python-3.x - Python 36 - 返回错误数据 - 异常几乎没用?
- powershell - 如何在 powershell 数组中写入托管元素的单个属性?
- python - 无法编译循环中的所有模式
- c# - 如何按顺序调用返回的代表?
- core-data - NSPredicate 检测存在于多个实体关系中的一整组实体