-
-
Save AndrewPix/b9bbc6ab07bcee47154e1246510b59a5 to your computer and use it in GitHub Desktop.
Automatically login 'admin' user in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIDDLEWARE_CLASSES = ( | |
'django.middleware.common.CommonMiddleware', | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.middleware.csrf.CsrfViewMiddleware', | |
'utils.AutomaticLoginUserMiddleware', | |
'django.contrib.auth.middleware.AuthenticationMiddleware', | |
'django.contrib.messages.middleware.MessageMiddleware', | |
'django.middleware.transaction.TransactionMiddleware', | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This middleware tries to automatically login the 'admin' user | |
# using the 'admin' password. DON'T USE THIS IN A MULTI-USER SYSTEM... | |
# ...but this is of great help on development :-D | |
# | |
# You must create an user named 'admin', with password 'admin' | |
# | |
class AutomaticLoginUserMiddleware(object): | |
def process_request(self, request): | |
user = auth.authenticate(username='admin', password='admin') | |
if user: | |
request.user = user | |
auth.login(request, user) | |
# | |
# This version automatically creates the user | |
# | |
class AutomaticLoginUserMiddleware2(object): | |
def process_request(self, request): | |
if request.user.is_authenticated(): | |
return | |
if not request.path_info.startswith('/admin'): | |
return | |
try: | |
User.objects.create_superuser('admin', '[email protected]', 'admin') | |
except: | |
pass | |
user = auth.authenticate(username='admin', password='admin') | |
if user: | |
request.user = user | |
auth.login(request, user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment