Issue
After I create superuser in command line, it says the superuser is created successfully, but when I try to log in it says "Please enter the correct email address and password for a staff account. Note that both fields may be case-sensitive." I tried to delete all migrations and database and try again but it did not help.
Here is my 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
Here is my 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)
Solution
In:
def create_user(self, Email, Password = None, **other_fields):
Remove = None, leaving Password only.
And add password=password as below:
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)
Hope it fixes it. If you still cannot log in, run python manage.py createsuperuser and create a new superuser and try to log in with that one.
Answered By - ana_london
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.