pseudonym117 pseudonym117 - 1 month ago 23
Python Question

Django Admin without Authentication

Is there a ready way to use the Django admin page without any form of authentication? I know I can use this method, but that was for Django 1.3. Are there any changes that would let me do this more easily in Django 1.6?

My main motivation for this is that I want to have as few database tables as possible, and I am using this only locally, so there is no need for any sort of authentication (I'm only ever running the server on localhost anyways).

Answer

Create a module auto_auth.py:

from django.contrib.auth.models import User

class Middleware(object):
    def process_request(self, request):
        request.user = User.objects.filter()[0]

Edit MIDDLEWARE_CLASSES in your settings.py:

  • Remove 'django.contrib.auth.middleware.AuthenticationMiddleware'
  • Add 'auto_auth.Middleware'

You can change User.objects.filter()[0] to something else if you want a particular user.


In response to your comment: yes. Try this:

class User:
    is_superuser = True
    is_active = True
    is_staff = True
    id = 1

def return_true(*args, **kwargs):
    return True
User.has_module_perms = return_true
User.has_perm = return_true

class Middleware(object):
    def process_request(self, request):
        request.user = User()

And remove 'django.contrib.auth' from INSTALLED_APPS

But if you use any apps that depend on the auth app, you're going to have a bad time.