In the previous article , we learned how to write two models
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:
12345678910111213INSTALLED_APPS = ('django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites','django.contrib.messages','django.contrib.staticfiles',# Uncomment the next line to enable the admin'django.contrib.admin',# Uncomment the next line to enable admin documentation'django.contrib.admindocs','myblog',)
- Uncomment the following lines in
123456from django.contrib import adminadmin.autodiscover()url(r'^admin/doc/', include('django.contrib.admindocs.urls')),url(r'^admin/', include(admin.site.urls)),
Now, you should have a
myblog/urls.pythat looks like this:1234567891011121314151617from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()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:
123456% python manage.py syncdbCreating tables ...Creating table django_admin_logInstalling custom SQL ...Installing indexes ...Installed 0 object(s) from 0 fixture(s)
Since the admin application
django.contrib.adminhas been uncommented in
myblog/settings.py, you should re-run the
syncdbcommand to create the database tables for it. Notice that a new table
django_admin_loghas been created by this command.
Have Fun with the Admin Site
Now, you can access the admin site by:
- Run the website:
12345678% python manage.py runserverValidating models...0 errors foundApril 05, 2013 - 12:08:17Django version 1.5, using settings 'myblog.settings'Development server is running at http://127.0.0.1:8000/Quit the server with CONTROL-C.
- 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:
>>> from django.contrib.auth import models as m
>>> superuser = m.User.objects.all()
Once you login, you should be able to see the following generic admin home page:
Notice that the models
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 http://127.0.0.1:8000/admin and see that the models
Comment have been added into the myblog application.
Modify the models using the admin site
Now you can click "Comments" to inspect the list of comments stored in the database:
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:
Now you can change any of the field to a value you like:
After you have filled your changes, you can click "Save" to persist your changes into the database:
Several points are worth notice:
- The forms for changing models such as
Commentare automatically generated from the models defined in
myblog/models.pyand registered in
- The different model field types such as type
Comment.messagecorrespond to different kinds of HTML
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.