Step 8: Write the Admin

If you want to blog, you need a back end interface that allows you to do that.  With Django, the admin interface is included with very little fuss. 

First, lets create an admin file in the blog application to handle our… blog.  If you are in Terminal, in your zing directory, type 'cd blog' to get you back in the blog directory.  Now, type "emacs admin.py" to create and open your admin.py file, the file that is going to tell Django what we want for our admin interface for the blog. 

Now, in that file, type the following content:

from django.contrib import admin
from zing.blog.models import *

class PostAdmin(admin.ModelAdmin):
    filter_horizontal = ('tags',)
    list_display = ('title', 'published',)
    prepopulated_fields = {'slug': ('title',)}

admin.site.register(Category)
admin.site.register(Tags)
admin.site.register(Post, PostAdmin)

Try to have a look through that code as well and understand it.  At the top, in the second line we imported our blog models that we created before (Tags, Category and Post).  The PostAdmin class was just there to tell it how to render some of the fields.  Take note of the last line of that class.  We are telling the admin site to pre-populate the 'slug' field with the contents of the title field.  This is very nice.  It means that when you type a title for the blog, it is going to just fill in the slug field for you. 

The last three lines should be obvious, registering each site to be included with the admin interface. 

Okay, a couple small things left to do.  First, go back to the 'zing' directory and open up your settings.py file in your text editor again. 

Now, scroll right to the bottom, and look at the last few lines of the INSTALLED_APPS bit.  See the two commented lines, delete the top one, and uncomment the second like this: 

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

Close that file and open one we haven't touched so far, the urls.py.  First, before we start to change anything, read all the contents. 

Okay, now you can actually just delete everything except the lines that I show below:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
)

The url file allow us to map a given url to the content we want it to put out.  This is very different to a normal website, but we'll get into that more later.  For now just save the file.  You can see that most of what we left behind has to do with the admin site. 

Now, we need to add the admin stuff to the database. 

You can type 'python manage.py validate' again in your Terminal just to make sure all is well.  Then run 'python manage.py syncdb' again. 

Now, open a second terminal, and change to the zing directory ('cd project/zing' will get you there).  Once in the 'zing' directory, type:

vernon@slick:~/project/zing$ python manage.py runserver

This was our free server, included with Django.  It is not a heavy lifter, so don't use it to serve your site on the Internet.  But for development, it is great. 

Now for some fun.  Open Firefox again, and enter the following url: http://127.0.0.1:8000/

Okay, you get an error?  If it is saying "Page not found" you are fine.  If it is some other error, Django gives you heaps of info in there to try to figure out what you did. 

Assuming you got through all that, add 'admin/' to the end of the url, and this time you should get a login screen. 

Remember the superuser we created above… log in with that user name and password.  You get greeted with this fantastic admin page. 

See the 'Blog' section… we did that!  Okay, now click on 'Posts'.  What do you see?  "0 posts"  Of course, we haven't added any blog posts yet. 

In the upper right and side of the page, click on 'add post'.  This is the page you are going to come to each time you want to create a blog post.  Okay, be patient, we'll be back here in no time. 

Close it all up.  We have built the back end, now lets get to writing the actual blog app logic. 


Share |
« Previous Page  |  Return To Contents Page  |  Next Page »

 

Thank you for visiting

This is my personal blog, but I try to write things that you may find interesting or useful.  I'm currently working on a six month long project digging into the elements that make a blog meaningful. For more on that, visit MEANINGFUL BLOG PROJECT page

You may want to know a little about me personally, so start at the about page. 

If you find some of what is here interesting, or you want to follow the project through, why not subscribe.

Tweet