The Admin application

Next, we will enable the admin application so that we can enter, edit and delete data in our app.

This application is already included in Django.

Register your own application to the Admin

In order that the admin can be used with our application, we need to make our models known to the admin.

This requires the file admin.py to be created in the application. The project will then look like this:

cookbook
|-- cookbook
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- manage.py
|-- recipes
|   |-- __init__.py
|   |-- admin.py
|   |-- models.py
|   |-- tests.py
|   `-- views.py
|-- static
`-- templates

Then you open the file in your editor and add the following two lines of code:

from django.contrib import admin

from .models import Category, Recipe

The admin module and the models of the application are now available.

Note

The second import is a relative import. These were defined in PEP 328 and introduced in Python 2.6.

Next, we create a class in order to register the model Category with the admin:

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ['name']}


admin.site.register(Category, CategoryAdmin)

There is nothing else to do.

The attribute prepopulated_fields helps the admin application to fill the field slug automatically as you type. In this case, the name attribute of the model is used.

That’s what we do now for the model Recipe:

class RecipeAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ['title']}


admin.site.register(Recipe, RecipeAdmin)

The complete file

The file admin.py should look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django.contrib import admin

from .models import Category, Recipe


class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ['name']}


class RecipeAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ['title']}


admin.site.register(Category, CategoryAdmin)
admin.site.register(Recipe, RecipeAdmin)

Activate the admin application

To activate the admin application, two steps are necessary.

Customizing the configuration

In the file settings.py remove the comment before the line 'django.contrib.admin' in INSTALLED_APPS to enable the admin application.

Customize URLconf

Thus the admin application can also be accessed in the browser we must also enable the URL of the admin.

Jump to the file cookbook/urls.py and uncomment the emphasized lines. Then the file looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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'^$', 'cookbook.views.home', name='home'),
    # url(r'^cookbook/', include('cookbook.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)),
)

Table Of Contents

Previous topic

The first Django App

Next topic

Database and Development Web Server

This Page

Languages

Support

If you love django-workshop, consider making a small donation on Flattr:

Useful Links