Skip to content

Instantly share code, notes, and snippets.

@AndrewPix
Forked from hgdeoro/settings.py
Created July 25, 2016 07:55
Show Gist options
  • Save AndrewPix/b9bbc6ab07bcee47154e1246510b59a5 to your computer and use it in GitHub Desktop.
Save AndrewPix/b9bbc6ab07bcee47154e1246510b59a5 to your computer and use it in GitHub Desktop.
Automatically login 'admin' user in Django
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 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', 'admin@example.com', '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