Step 11: Write the Urlconf
Okay, close all those files with templates and all that. We are now going to write our urls. Now, if you have followed along diligently, you have already 'written' the urls in the views as a comment starting each view. We now have to got the the urls.py file and map them to those views.
Open your urls.py file in the main 'zing' directory. Add the line in red.
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('', (r'^admin/', include(admin.site.urls)),
(r'^', include('zing.blog.urls')),
)
Actually, all we have done now is tell the urls.py file that anything that doesn't map to the admin site, we'll deal with under 'blog.site.urls'.
Well, in order to do that, we need to write that python file. One thing always to remember is that it is all just Python, and so you can get creative. You can change file names, use different directories, whatever you want, so long as you import the right thing when you need it.
As a rule of thumb, I would say, stick to Django's conventions unless it is actually more clear not to. Most of the time, if you start to change things around, you are going to confuse yourself.
In this case we aren't doing something radical, we are just writing some of our views in the 'blog' directory.
Okay, go into your 'blog' directory, where you have the views.py, models.py and admin.py files and create a new one called 'urls.py'.
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^$', list),
(r'^archive/(?P\d{1,2}/$', list),
(r'^\d{4}/d{1,2}/(?P.*)/$', detail),
(r'^(?P\d{4})/(?P \d{1,2})/$', month),
(r'^(?P\d{4})/$', year),
(r'^category/$', category), (r'^category/(?P.*)/$', one_category), (r'^tag/$', tag), (r'^tag/(?P .*)/$', one_tag), )
Depending on your familiarity with Python, this shouldn't be to hard at this point to figure out. The regular expression (which I will not cover here, but if you don't know what it is, do look it up - it is fun) maps to a specific url.
Simply put, when someone requests a specific url, that url is checked in this file and when a match is found, it is sent to the view, from were the page is rendered by passing values to the templates.
We know which view to use because we specify it at the end of each of the tuples within the patterns tuple. So, if you ask for the home page, it is going to match the first one (because there is nothing beyond the domain name, which is truncated for the urls. So, it will use the 'list' function in the views.py file, which, in our case, will use the 'list.html' template to extend the 'base.html' template.
Now, if all has gone well, you have a complete, working website.
If you open your terminal, and type runserver, you should be able to try out each url.
vernon@slick:~/project/zing$ python manage.py runserver
If you are having problems, look carefully at the error messages that Django is giving you. Note, you are not going to be getting your 404 and 500 pages that you wrote yet, we'll get to that when we start deploying the site on a server. For now, Django's error messages are much more useful, they are going to tell you what you did wrong (or do their best to try.) Ask me questions, and I'll help where I can, but also dig into Django's documentation a little.
Resist the urge, I think, to get to creative at this point. The tough part is getting everything to work well with a proper web-server, which we'll get to in a bit.