'Django can't login the superuser in admin page
So , I am trying to make an register and login system . I want to customize it using the custom user model , where I can save my data and extend the model. I am using AbstractBaseUser and BaseUserManager to do that. The problem is I can't login in the django admin page. I created already a superuser so I can access admin section , I enter the Email and Password correct and then I have this error : "Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive." What my goal is , is that I want to be able to access the administration by login ( i mean fixing this red message and being able to use administration as an superuser ), and then add to the Userr model the specialkey field which will take place in the login view as the username in the built-in django authentication system.
This is the message error : https://i.stack.imgur.com/TL1wT.png.
I hope someone helps me with this , because I can't see the problem in the code , I've been trying to fix this for almost 3 days.
The models.py:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser , BaseUserManager
class UserrManager(BaseUserManager):
def create_user(self , email , username , password = None):
if not email:
raise ValueError ("Users need an email.")
if not username:
raise ValueError ("Users need an username")
#if not specialkey:
# raise ValueError ("Users need an specialkey")
user = self.model(
email = self.normalize_email(email),
username = username ,
)
user.set_password(password)
user.save(using = self._db)
return user
def create_superuser(self , email , username , password , ):
user = self.create_user(
username = username ,
email = self.normalize_email(email),
password = password,
)
user.is_staff = True
user.is_superuser = True
user.is_active = True
user.save(using = self._db)
return user
class Userr(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length = 60 , unique = True)
username = models.CharField(max_length=30, unique = True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
# specialkey = models.CharField(max_length=26 , unique = True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = [ 'username' , ]
objects = UserrManager()
def __str__ (self):
return self.username
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
The settings.py:
Django settings for reglog project.
Generated by 'django-admin startproject' using Django 3.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2m9*9)#@3r%9s05+8lc+7q99r0v2!+-pey_axttbn4anh=h7^4'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
'rlsystem',
'website',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'reglog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
AUTH_USER_MODEL = 'rlsystem.Userr'
WSGI_APPLICATION = 'reglog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.RemoteUserBackend',
)
Solution 1:[1]
Had the same issue. Instead of password=None, I changed it to password and passed 'password=password' together with 'username=username' into the create_user functions as you see below:
class MyAccountManager(BaseUserManager): def create_user(self, email, username, password): ....... .......
user = self.model(email=self.normalize_email(
email), username=username, password=password)
.......
def create_superuser(self, email, username, password):
user = self.create_user(email=self.normalize_email(
email), username=username, password=password)
.......
If you still cannot log in, run python manage.py createsuperuser and create a new superuser and try to log in with that one.
Hope it works for you
Solution 2:[2]
I would suggest to read this
from the Django documentation.
Please confirm that the user is active is_active because if the user is not active you will get the error. I got the same error I forgot to login with user that is active
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ana_london |
Solution 2 | FLAK-ZOSO |