我已经创建了自定义用户模型,但无法使用有效的电子邮件id和密码登录
也添加了用户管理器。用户管理器有问题吗?我还在settings.py中添加了AUTH_USER_MODEL
models.py
class UserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, email, password):
"""
Creates and saves a staff user with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.staff = True
user.save(using=self._db)
return user
def create_superuser(self, email, password):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.staff = True
user.admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField(verbose_name='email address',
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False) # a superuser
objects = UserManager()
# notice the absence of a "Password field", that is built in.
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
编写了has_perm、has_module_perm等方法
forms.py
这是注册表
from django import forms
from django.contrib.auth import get_user_model
User = get_user_model()
class RegisterForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}))
password_2 = forms.CharField(label='Confirm Password',widget=forms.PasswordInput(attrs={'class':'form-control'}))
class Meta:
model = User
fields = ['email','password','password_2']
labels = {
'email':'Email Id',
'password':'Password',
}
widgets = {
'email':forms.EmailInput(attrs={'class':'form-control'}),
}
def clean_email(self):
'''
Verify email is available.
'''
email = self.cleaned_data.get('email')
qs = User.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError("Email is taken")
return email
def clean(self):
'''
Verify both passwords match.
'''
cleaned_data = super().clean()
password = cleaned_data.get("password")
password_2 = cleaned_data.get("password_2")
if password is not None and password != password_2:
self.add_error("password_2", "Your passwords must match")
return cleaned_data
forms.py
class AuthenticationForm(forms.Form):
email = forms.EmailField(widget=forms.TextInput(
attrs={'class': 'form-control','type':'text','name': 'email','placeholder':'Email'}),
label='Email')
password = forms.CharField(widget=forms.PasswordInput(
attrs={'class':'form-control','type':'password', 'name': 'password','placeholder':'Password'}),
label='Password')
class Meta:
fields = ['email', 'password']
这是注册视图。更新了post
def registerEmployeeView(request):
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
messages.success(request,'Employee Register Successfully')
return redirect('login-employee')
else:
form = RegisterForm()
context = {
'form':form,
}
return render(request,'accounts/register.html',context)
views.py
def loginEmployeeView(request):
if request.method == 'POST':
form = AuthenticationForm(data = request.POST)
if form.is_valid():
print("1")
email = request.POST.get('email')
password = request.POST.get('password')
print("2")
user = authenticate(email=email, password=password)
print("3")
if user is not None:
if user.is_active:
print("4")
login(request,user)
messages.success(request,'Logged in successfully')
return redirect('dashboard')
else:
messages.error(request,'user is not active')
return redirect('login-employee')
else:
messages.error(request,'invalid username or password')
return redirect('login-employee')
else:
print("5")
form = AuthenticationForm()
return render(request,'accounts/login.html',{'form':form,})
我尝试用有效的信息登录,但它显示无效的用户名或密码。谢谢你。
您应该使用不同的身份验证后端来检查给定用户对象的凭据。这样的后端看起来像:
# app_name/backend.py
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
UserModel = get_user_model()
class EmailBackend(ModelBackend):
def authenticate(self, request,email=None, password=None):
try:
user = UserModel.objects.get(email=email)
except UserModel.DoesNotExist:
UserModel().set_password(password)
return None
if user is not None and user.check_password(password):
if user.is_active:
return user
return None
然后在设置文件中,你应该使用EmailBackend
作为AUTHENTICATION_BACKENDS
设置(Django-doc):
# settings.py
# ⋮
AUTHENTICATION_BACKENDS = [
'app_name.backends.EmailBackend'
]
# ⋮
最后,您的Form
应该使用.create_user(…)
方法,或者使用.set_password()
:
class RegisterForm(forms.ModelForm):
# ⋮
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user