django 为什么dajngo是问我密码3次在自定义用户模型Singup

l7wslrjt  于 2023-01-03  发布在  Go
关注(0)|答案(1)|浏览(114)

嗨,我想我搞砸了我的自定义用户创建系统。前一段时间它工作正常,但我不知道我做了什么,它结束了搞砸了。现在为了创建一个新用户3密码字段是必需的
我有一个自定义用户模型,使用电子邮件而不是用户名,如下所示:

from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _

class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)

class User(AbstractUser):
    """User model."""

    username = None
    email = models.EmailField(_('email address'), unique=True)
    phone = models.CharField(_('phone_number'),max_length=50,unique=True,null=True,blank=True)
    notify_email = models.BooleanField(_('notify_email'),default=False)
    notify_whats = models.BooleanField(_('notify_whats'),default=False)
 

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

我的form.py文件是这样的:

from django import forms
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm, UserChangeForm
from django.forms.widgets import PasswordInput, TextInput
from .models import User


class CustomAuthForm(AuthenticationForm):
    username = forms.CharField(widget=TextInput(attrs={
        'type':'text',
        'class':'form-control',
        'placeholder': 'Email',
        }))
    password = forms.CharField(widget=PasswordInput(attrs={
        'type':'password',
        'class':'form-control',
        'placeholder':'Password',
        }))

class SignUpForm(UserCreationForm):
    username = forms.EmailField(widget=TextInput(attrs={
        'type':'text',
        'class':'form-control',
        'placeholder': 'Email',
        }))
    password = forms.CharField(widget=PasswordInput(attrs={
        'type':'password',
        'class':'form-control',
        'placeholder':'Password',
        }))
    password1 = forms.CharField(widget=PasswordInput(attrs={
        'type':'password',
        'class':'form-control',
        'placeholder':'Reenter Password',
    }))
    class Meta:
        model = User
        fields = ('username',)

当我转到{{form.as_p }}这样的模板时,我以3个密码字段(全部必填)结束。
当我打印错误时,Django告诉我:密码,密码1,密码2是必需的。事实上,如果我填写所有字段,它会创建一个新用户,但没有电子邮件字段。我已经花了几个小时试图修复这个问题或试图完成重置我的用户模型,但我最终卡住了。任何人都可以帮助我understard正在发生什么?谢谢你的时间!x1c 0d1x

nzk0hqpo

nzk0hqpo1#

字段名为password1password2,而不是password。通过添加password字段,您将引入第三个字段。因此,您的表单应使用:

class CustomAuthForm(AuthenticationForm):
    username = forms.CharField(widget=TextInput(attrs={
        'type':'text',
        'class':'form-control',
        'placeholder': 'Email',
        }))
    password1 = forms.CharField(widget=PasswordInput(attrs={
        'type':'password',
        'class':'form-control',
        'placeholder':'Password',
        }))

class SignUpForm(UserCreationForm):
    username = forms.EmailField(widget=TextInput(attrs={
        'type':'text',
        'class':'form-control',
        'placeholder': 'Email',
        }))
    password1 = forms.CharField(widget=PasswordInput(attrs={
        'type':'password',
        'class':'form-control',
        'placeholder':'Password',
        }))
    password2 = forms.CharField(widget=PasswordInput(attrs={
        'type':'password',
        'class':'form-control',
        'placeholder':'Reenter Password',
    }))
    class Meta:
        model = User
        fields = ('username',)

相关问题