Welcome back to my tutorial on creating a blog with django! We are going to continue with the myblog project now that we have a Post model set up.

Installing the admin site

Open settings.py and add ‘django.contrib.admin’ to the installed applications as shown below:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'myblog.blog',
    'django.contrib.admin',
)

This will tell django to install the models included in the admin application. Let’s quickly install the models required. Open up the terminal and run the syncdb command again just like we did in part 2:

python manage.py syncdb

This time, it will simply create a table and an index for it. There is no mucking about with user accounts as that was already handled last time.

URL Routing for the Admin Site

We need to tell the site to route URLs to the admin site. Open up urls.py and replace the contents with the following:

from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
 
urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
)

Let’s take a quick break and examine this file. The first line imports some modules required for URL routing. Just make sure this line is in every urls.py file you make. The second line imports the admin site and the third line runs a function that is used for further processing behind the scenes. The urlpatterns variable defines our url routing rules. We will delve into this in a later part of the tutorial. For now, we want it to route all urls that start with admin/ to be processed by the admin site’s urls.py file. Take note that the URL is denoted as ‘^admin/’. It is in reality a regular expression that simply means that the URL starts with admin/. If we wanted to make the URL definite, we would add $ to the end, but in this case we don’t want to make the URL definite.

If you were to view your project right now, you would see a nice error page (404) and the admin site would look rather incomplete. Let’s complete the site first by registering our Post model with the admin site.

A Basic Registration

Create a new file called admin.py inside the blog directory/app in the myblog project. Open your new blog/admin.py file in your text editor and add the following code:

from django.contrib import admin
from myblog.blog.models import Post
 
admin.site.register(Post)

The first line imports the admin application. The second line imports our Post model. We then use the admin application to register our Post model on the last line. Save this file and let’s check our admin site out! Go back to the terminal and let’s start our development server.

python manage.py runserver

Now open your browser and go to http://127.0.0.1:8000/. You should be presented with a nice error page. Don’t panic! Everything is working properly. Take note that the URL configuration we defined is displayed on the page. This is an easy way to make sure URL routing is working properly. Let’s continue onto the admin site. Just add admin to the end of the address (http://127.0.0.1:8000/admin).

You should now see a django administration login page. Enter the username and password you created during part 2 into this form and you should be allowed into the admin site. If you forgot your username and password or didn’t create a user, you can try it again by deleting the database (dev.db) from the myblog folder and recreating it using the syncdb command.

Once on the admin site, you should see several sections. One section is our post model. Go ahead and mess around with the posts. Also, take a moment and stop by in the Users section to set a first name and last name on your user account. We will use the first name and last name fields in our templates later.

At this stage, you might notice that the admin site lacks some of the nice features that help make the site feel complete. Let’s go and redo our admin site implmentation.

Completing the Admin Site

Reopen admin.py and replace admin.site.register(Post) with the following:

class PostAdmin(admin.ModelAdmin):
    date_hierarchy = 'created'
    list_display = ('title', 'user', 'created', 'updated', 'is_published')
    list_filter = ('is_published',)
 
admin.site.register(Post, PostAdmin)

Now we actually define a few extra features for our admin site. The fire line creates a new ModelAdmin object called PostAdmin. The first property assigned to the PostAdmin object is date_hierarchy. By assigning this, we are telling it to give us a nice way of viewing posts chronologically by the time they were created. The second property defines what fields will be displayed on the page that lists our posts on the admin site. The third property defines the fields (or in this case, field) that we can filter the list by. Finally, we add the PostAdmin object on to our line that registers the Post model. By default, the Post model is assigned the standard ModelAdmin. By adding in the PostAdmin object, ModelAdmin is overridden and replaced by PostAdmin. Now, quit the development server with Control+C and restart it again:

python manage.py runserver

Open up the admin site in your browser again (http://127.0.0.1:8000/admin/) and go ahead and have some fun! Start off by creating some posts. I recommend making at least eleven posts as it will help when we start into pagination.

In part 4, we will start into creating the front end of the site.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
  • Add to favorites