[英]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.