Tutorial: The Admin Interface

Posted: March 17th, 2010 | Author: Adam | Filed under: Django, Python, programming | No Comments »

So we’ve setup our applicationinitilized our database and now we’re ready to take our first look at one of the cooler features in Django, the admin interface.

Let’s open up the urls.py file in the root project (blog/) directory. I’ve removed the some of the commented out lines below for brevity

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',

    # Uncomment the next line to enable the admin:
    # (r'^admin/', include(admin.site.urls)),
)<


Now just like the comments say we want to uncomment out the “from django.contrib…”, “admin.autodiscover()”, and the “(r^admin/…)” lines in urls.py so it looks like this

from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

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

Now, what does all of this do? The first two lines (the imports) are bringing in the needed modules and the admin. namespace for us. The urlspatterns line is something we’ll be coming back to. A simple explination of it is that this is where all the URL’s of our project will end up living. Right now there is one URL “admin/” that is pointing to another URL file “admin.site.urls”. This is the base URL’s of the Django admin site.

After we’ve saved our changes let’s run the development server again

$ python manage.py runserver
Validating models...
0 errors found

Django version 1.1 alpha 1, using settings 'blog.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

This time let’s goto the URL http://127.0.0.1:8000/admin/

Admin-Login

Well, now we’re getting somewhere. Log in with the superuser account that you created in step 3

Admin-Main

Go ahead poke around for a few minutes. Check out the users section. Create a new user! Create a group, check out the permissions. This all came from adding one line of code to your INSTALLED_APPS. Pretty cool huh? You smiling yet?

This admin screen isn’t just for Django created stuff like users and groups. This is where your data will eventually live too. So let’s go ahead to the next step and create some models for our blog application.

Download at this step: GitHub



Leave a Reply