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
anddjango.contrib.admindocs
inINSTALLED_APPS
inmyblog/settings.py
[python]
INSTALLED_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',
)
[/python] - Uncomment the following lines in
myblog/urls.py
:
[python]
from django.contrib import admin
admin.autodiscover()url(r'^admin/doc/', include('django.contrib.admindocs.urls')),url(r'^admin/', include(admin.site.urls)),
[/python]Now, you should have a
myblog/urls.py
that looks like this:[python]
from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.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)),
)
[/python] - Run the following command in your shell:
[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 applicationdjango.contrib.admin
has been uncommented inmyblog/settings.py
, you should re-run thesyncdb
command to create the database tables for it. Notice that a new tabledjango_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:
[shell]
% 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 http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[/shell] - 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:
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:
[python]
>>> from django.contrib.auth import models as m
>>> m.User.objects.all()
[]
>>> superuser = m.User.objects.all()[0]
>>> superuser.set_password('12345')
>>> superuser.save()
[/python]
Once you login, you should be able to see the following generic admin home page:
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:
[python]
from django.contrib import admin
from myblog import models as m
admin.site.register(m.Post)
admin.site.register(m.Comment)
[/python]
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.
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
Post
andComment
are automatically generated from the models defined inmyblog/models.py
and registered inmyblog/admin.py
- The different model field types such as type
DateTimeField
forComment.created_at
and typeTextField
forComment.message
correspond to different kinds of HTMLinput
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.