Activating the Admin Interface

We think the admin interface is the coolest part of Django—and most Djangonauts agree—but since not everyone actually needs it, it’s an optional piece in earlier versions of Django. That means there are three steps you’ll need to follow to activate it:

  • Add admin metadata to your models.

Not all models can (or should) be editable by admin users, so you need to “mark” models that should have an admin interface. You do that by adding an inner Admin class to your model (alongside the Meta class, if you have one). So, to add an admin interface to our Book model, we use this:

 

class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
num_pages = models.IntegerField(blank=True, null=True)

def __str__(self):
return self.title

class Admin:
pass

The Admin declaration flags the class as having an admin interface. There are a number of options that you can put beneath Admin, but for now we’re sticking with all the defaults, so we put pass in there to signify to Python that the Admin class is empty. If you’re following this example with your own code, it’s probably a good idea to add Admin declarations to the Publisher and Author classes at this point.

  • Install the admin application. Do this by adding “django.contrib.admin” to your INSTALLED_APPS
  • If you’ve been following along, make sure that “django.contrib.sessions”, “django.contrib.auth”, and “django.contrib.contenttypes” are uncommented, since the admin application depends on them. Also uncomment all the lines in the MIDDLEWARE_CLASSES setting tuple and delete the TEMPLATE_CONTEXT_PROCESSOR setting to allow it to take the default values again.
  • Run pythonpysyncdb. This step will install the extra database tables the admin interface uses.

Note – When you first run syncdb with “django.contrib.auth” in INSTALLED_APPS, you’ll be asked about creating a superuser. If you didn’t do so at that time, you’ll need to run django/contrib/auth/bin/create_superuser.py to create an admin user. Otherwise, you won’t be able to log in to the admin interface.

  • Add the URL pattern to your py. If you’re still using the one created by startproject, the admin URL pattern should be already there, but commented out. Either way, your URL patterns should look like the following:

from django.conf.urls.defaults import *

 

urlpatterns = patterns(”,
(r’^admin/’, include(‘django.contrib.admin.urls’)),
)

That’s it. Now run python manage.py runserver to start the development server. You’ll see something like this:

 

Validating models…
0 errors found.

Django version 0.96, using settings ‘mysite.settings’
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now you can visit the URL given to you by Django (http://127.0.0.1:8000/admin/ in the preceding example), log in, and play around.

Back to Tutorial

Servlet Configuration
Using the Admin Interface

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?