Hi I think I messed up my custom User creation system. Time ago it works fine but I dont know what I did, and it ended messed up. Now in order to create a new user 3 PASSWORDS fiels are required
I have a custom user model that uses email instead of username like this:
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()
My form.py file is like this:
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',)
And when I pass to the templete like {{ form.as_p }} I ended with 3 password fields ALL REQUIRED.
When I print errors Django tells me: password, password1,password2 are required. In fact if I fill all fields, It creates a new user but with no email field. I have been hours trying to fix this problem or trying to complete reset my user model but I ended stuck. Anyone can help me understard what is happening? THANKS FOR YOUR TIME!
CodePudding user response:
The field names are password1
and password2
, not . By adding a password
password
field, you thus introduce a third field. Your form thus should work with:
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',)