This article is part of in the series
Last Updated: Wednesday 29th December 2021

In the previous article , we learned how to write two models Post and Comment for your Django application myblog. In this article, we are going to learn how to activate Django's automatic admin site that provides a convenient interface for users or administrators of your website myblog to create, read, update and delete (CRUD) the content of your website. CRUD operations are the four basic functions of persistent storage. The acronym CRUD describes a set of operations that is almost common in any database-based computer application. Since these operations are common knowledge, we're going to use CRUD in this series from now on.

What is the Django Admin Site and How to Activate it

The Django admin site is an application that comes as a default with any new Django project created by the command django-admin.py startproject. Since it's almost a given that any database-powered website should allow users to CRUD its content, Django provides a ready-to-use admin site that is part of any new Django project.

By default, the Django admin site is not activated because not all programmers want to enable it. In order to enable it, you need to do three things:

  • Uncomment django.contrib.admin and django.contrib.admindocs in INSTALLED_APPS in myblog/settings.py
    # Uncomment the next line to enable the admin
    # Uncomment the next line to enable admin documentation
  • Uncomment the following lines in myblog/urls.py:
    from django.contrib import admin
    admin.autodiscover()url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    url(r'^admin/', include(admin.site.urls)),

    Now, you should have a myblog/urls.py that looks like this:

    from django.conf.urls import patterns, include, url

    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin

    urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'myblog.views.home', name='home'),
    # url(r'^myblog/', include('myblog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

  • Run the following command in your shell:
    % python manage.py syncdb
    Creating tables ...
    Creating table django_admin_log
    Installing custom SQL ...
    Installing indexes ...
    Installed 0 object(s) from 0 fixture(s)
    [/shell]Since the admin application django.contrib.admin has been uncommented in myblog/settings.py, you should re-run the syncdb command to create the database tables for it. Notice that a new table django_admin_log has been created by this command.

Have Fun with the Admin Site

Now, you can access the admin site by:

  • Run the website:
    % python manage.py runserver
    Validating models...0 errors found
    April 05, 2013 - 12:08:17
    Django version 1.5, using settings 'myblog.settings'
    Development server is running at
    Quit the server with CONTROL-C.
  • Open a web browser and navigate to You should be able to see the login screen of admin:
    Django Admin Login Page

Now you can login using the credentials of the superuser you create in the article . If you don't remember the password of your superuser, you can modify its password by:

>>> from django.contrib.auth import models as m
>>> m.User.objects.all()
>>> superuser = m.User.objects.all()[0]
>>> superuser.set_password('12345')
>>> superuser.save()

Once you login, you should be able to see the following generic admin home page:
Django Admin Home Page Without Myblog

Notice that the models Post and Comment are not listed on this page. So, we should add them by creating a new file admin.py in directory myblog/ which looks like this:

from django.contrib import admin
from myblog import models as m


Now, restart the Django development server by terminating the command python manage.py runserver and re-run it. Although the Django development server listens to file changes within the directory myblog/ and auto-restart every time there's a file change, it does not auto-restart if there's a file creation.

Now you can access the page and see that the models Post and Comment have been added into the myblog application. Django Admin Home Page

Modify the Models Using the Admin Site

Now you can click "Comments" to inspect the list of comments stored in the database:
Django Admin Comments Page

Click the first "Comment object" will show a HTML form-based page that allows you to modify the message Text field, the created Datetime field and the post Foreign Key field:
Django Admin Page Modify Comment Object

Now you can change any of the field to a value you like:
Django Admin Page Modify Comment Object Continue

After you have filled your changes, you can click "Save" to persist your changes into the database:
Django Admin Page Modify Comment Object Persisted

Several points are worth notice:

  • The forms for changing models such as Post and Comment are automatically generated from the models defined in myblog/models.py and registered in myblog/admin.py
  • The different model field types such as type DateTimeField for Comment.created_at and type TextField for Comment.message correspond to different kinds of HTML input elements.

Summary and Insights

We have activated Django's Admin Site with a couple lines of code and the resulting URL "/admin" provides a fully functional web interface for administrators of the website to CRUD any model that is registered through admin.site.register() function. Although it might look like magic, it is a manifestation of the Don't-Repeat-Yourself (DRY) principle. The code that we wrote to activate the admin site is kept to an absolutely minimum and you could certainly imagine the amount of poiler-plate code which we have to write if Django does not provide an admin site.

In the following articles of this series, we will visit and investigate the DRY principle over and over again. Since DRY promotes code-reusability and design-towards-maintainability, Django's developers used it as a guideline to implement the Django framework and we should also keep it mind when we are writing software.

About The Author