34π
This problem may be related to the Authentication Backends.
Please check your settings files for the AUTHENTICATION_BACKENDS parameter.
Try the following value:
AUTHENTICATION_BACKENDS = (
('django.contrib.auth.backends.ModelBackend'),
)
More information on the
Official Django Documentation
3π
Try this; in tests.py:
from django.contrib import auth
class AuthTestCase(TestCase):
def setUp(self):
self.u = User.objects.create_user('test@dom.com', 'test@dom.com', 'pass')
self.u.is_staff = True
self.u.is_superuser = True
self.u.is_active = True
self.u.save()
def testLogin(self):
self.client.login(username='test@dom.com', password='pass')
Then run the test with python manage.py test <your_app_name>.AuthTestCase
. If this passes, the system is working, maybe look at the username and password to make sure they are acceptable.
- [Django]-Django: using objects.values() and get ForeignKey data in template
- [Django]-How to force application version on AWS Elastic Beanstalk
- [Django]-Altering database tables in Django
2π
I had the same issue, but AUTHENTICATION_BACKENDS flag on settings file was not the problem for me. Using Django Rest Framework somehow i had modified the password without calling set_password therefore bypassing hashing the password. Thatβs why it was showing the invalid login.
I was able to detect the issue by running simple test in order to test the user creation by a similar test:
from django.test import TestCase
from django.contrib import auth
from .models import *
class AuthTestCase(TestCase):
def setUp(self):
self.u = UserProfile.objects.create_user('test@dom.com', 'iamtest', 'pass')
self.u.is_staff = True
self.u.is_superuser = True
self.u.is_active = True
self.u.save()
def testLogin(self):
self.client.login(username='test@dom.com', password='pass')
It is also worth mentioning that I was creating a custom user named UserProfile
- [Django]-Django Admin β Disable the 'Add' action for a specific model
- [Django]-Group by Foreign Key and show related items β Django
- [Django]-How to select_related when using .get() in django?
1π
Are you using a custom user model and forgot add it in settings.py
? That is what just happened to me.
# Substituting a custom User model
AUTH_USER_MODEL = "app_custom_auth.User"
- [Django]-Best Fabric scripts for Django
- [Django]-Complete django DB reset
- [Django]-Printing Objects in Django
1π
You just need to delete the db.sqlite3
and migrate again (python manage.py migrate
) then do:
python manage.py createsuperuser
to create the account again.
- [Django]-Django Admin's "view on site" points to example.com instead of my domain
- [Django]-Limit number of model instances to be created β django
- [Django]-How to merge consecutive database migrations in django 1.9+?
0π
You can do the following:
- Enter your mysql (or other database console)
- USE YourDATABASE;
- SELECT * from auth_user;
- watch is_staff and is_superuser item
- UPDATE auth_user SET is_staff = β1β where username = βrootβ;
Then you can login again!
- [Django]-Adding a jQuery script to the Django admin interface
- [Django]-Where's my JSON data in my incoming Django request?
- [Django]-GeoDjango GEOSException error
0π
The answer is :
def create_superuser(self, username, email, password=None, **extra_fields):
user = self.create_user(username, email, password=password, is_staff=True, **extra_fields)
user.is_active = True
user.save(using=self._db)
return
- [Django]-Select distinct values from a table field
- [Django]-Django: detect admin login in view or template
- [Django]-Django-orm case-insensitive order by
0π
is_active
and is_staff
must be True
to log in Django Admin so in these cases below, you can log in Django Admin:
is_active β
is_staff β
is_superusr
is_active β
is_staff β
is_superusr β
In addition, even if is_superusr
is True
, you cannot log in Django Admin in these cases below because making is_superusr
True
gives the user all permissions except logging in Django Admin:
is_active
is_staff β
is_superusr β
is_active β
is_staff
is_superusr β
is_active
is_staff
is_superusr β
- [Django]-Complete django DB reset
- [Django]-Django logout redirects me to administration page
- [Django]-Django post_save preventing recursion without overriding model save()
0π
Another issue that can cause this is custom middleware, as it can interfere with the request/response cycle. This interference might not always throw explicit errors but can prevent processes like authentication from completing successfully.
Try disabling any custom middleware in settings.py
and see if that solves your problem.
For example, converting the default Django WSGIRequest to a DRF βRequestβ object has side effects that can cause standard Django views (including Django Admin) to have issues. For example, the below custom_logging_middleware.py
would cause issues logging into Django Admin:
from rest_framework.request import Request
import logging
class RequestLoggingMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
start_time = time.time()
logger = logging.getLogger(__name__)
# Wrap the original request with DRF's Request, since the standard Django WSGIRequest doesn't have a .data attribute
drf_request = Request(request)
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
- [Django]-Django: how to annotate queryset with count of filtered ForeignKey field?