[英]Can't login to Django admin panel after successfully creating superuser using Django custom user model
在命令行中创建超级用户后,它说超级用户创建成功,但是当我尝试登录时,它说“请输入正确的 email 地址和员工帐户密码。请注意,这两个字段可能区分大小写。” 我试图删除所有迁移和数据库并重试,但没有帮助。
这是我的 model.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db.models.deletion import CASCADE
from django.utils import timezone
from django.utils.translation import gettext_lazy
# Create your models here.
class UserAccountManager(BaseUserManager):
def create_user(self, Email, Password = None, **other_fields):
if not Email:
raise ValueError(gettext_lazy('You must provide email address'))
email = self.normalize_email(Email)
user = self.model(Email=email , **other_fields)
user.set_password(Password)
user.save(using=self._db)
return user
def create_superuser(self, Email, Password = None, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
return self.create_user(Email=Email, Password = Password, **other_fields)
class Customer(AbstractBaseUser, PermissionsMixin):
Email = models.EmailField(gettext_lazy('email address'), max_length=256, unique=True)
Name = models.CharField(max_length=64, null=True)
Surname = models.CharField(max_length=64, null=True)
Birthday = models.DateField(auto_now=False, null=True,blank=True)
PhoneNumber = models.CharField(max_length=16, unique=True, null=True, blank=True)
Address = models.CharField(max_length=128, blank=True)
RegistrationDate = models.DateTimeField(default=timezone.now, editable=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
objects = UserAccountManager()
USERNAME_FIELD = 'Email'
REQUIRED_FIELDS = []
def __str__(self):
return self.Name + " " + self.Surname
def has_perm(self, perm, obj = None):
return self.is_superuser
这是我的 admin.py
from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.core.exceptions import ValidationError
from .models import *
# Register your models here.
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = Customer
fields = ('Email', 'Name', 'Surname')
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = Customer
fields = ('Email', 'Name','Surname','Birthday','PhoneNumber','Address', 'is_staff', 'is_active', 'is_superuser')
def clean_password(self):
return self.initial["password"]
class UserAdmin(BaseUserAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ('Email', 'Name', 'Surname')
list_filter = ('is_superuser','Email', 'Name', 'Birthday')
fieldsets = (
(None, {'fields': ('Email', 'password')}),
('Personal info', {'fields': ('Birthday','Name','Surname')}),
('Permissions', {'fields': ('is_superuser',)}),
)
add_fieldsets = ()
search_fields = ('Email',)
ordering = ('Email',)
filter_horizontal = ()
admin.site.register(Customer,UserAdmin)
在:
def create_user(self, Email, Password = None, **other_fields):
删除 = 无,仅留下密码。
并添加密码=密码如下:
user = self.model(email=self.normalize_email(
email), username=username, password=password)
user = self.create_user(email=self.normalize_email(
email), username=username, password=password)
希望它能修复它。 如果仍然无法登录,请运行 python manage.py createsuperuser 并创建一个新的超级用户并尝试使用该超级用户登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.