This article is part 4 of 14 in the series Python Django Tutorial

Last Updated: Thursday 12th December 2013

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 following lines in myblog/urls.py:

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

  • Run the following command in your 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:
  • Open a web browser and navigate to http://127.0.0.1:8000/admin. 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:

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:

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 http://127.0.0.1:8000/admin 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

  • michael kors outlet

    good post, i have been looking for it