Django URL syntax

Summarized from :

1. non-named regular-expression groups

from django.conf.urls.defaults import * #Don't forget this

urlpatterns = patterns('',
    (r'^articles/2003/$', 'news.views.special_case_2003'), #r = raw, ^ = start with, $ = end with, leading '/' isn't neccessary
    (r'^articles/(\d{4})/$', 'news.views.year_archive'), #ex. /articles/2008
    (r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), #ex /articles/2008/07
    (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), #ex /articles/2008/07/5555555 (any no. of digits)


(parenthesis) = capture string as a paremeter and pass into the view function

2. named regular-expression groups

urlpatterns = patterns('',
    (r'^articles/2003/$', 'news.views.special_case_2003'),
    (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
    (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
    (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),

(?P<name>pattern) = capture a string and pass the paremeter as  name="xxx"

Note : * slightly more explicit and less prone to argument-order bugs — and you can reorder the arguments in your views’ function definitions*

3. URL with GET parameters

For example, in a request to, the URLconf will look for myapp/.

In a request to, the URLconf will look for myapp/.

The URLconf doesn’t look at the request method


  • Always use patterns() to create the urlpatterns variable.
  • In Django 1.0 You can use the url() function, instead of a tuple, as an argument to patterns(). This is convenient if you want to specify a name

5. handler404

if none of the URL patterns match, use 'django.views.defaults.page_not_found'.

6. handler500

in any case of server errors, use 'django.views.defaults.server_error'.

7. handle more than 1 url with the same view (but different argument)

# URLconf
urlpatterns = patterns('',
    (r'^blog/$', ''),
    (r'^blog/page(?P<num>\d+)/$', ''),

# View (in blog/
def page(request, num=”1″):
# Output the appropriate page of blog entries, according to num.

8.view prefix… make it shorter

urlpatterns = patterns(‘’,
(r’^articles/(\d{4})/$’, ‘year_archive’),
(r’^articles/(\d{4})/(\d{2})/$’, ‘month_archive’),
(r’^articles/(\d{4})/(\d{2})/(\d+)/$’, ‘article_detail’),

9. multiple prefix

from django.conf.urls.defaults import *

urlpatterns = patterns('django.views.generic.date_based',
    (r'^$', 'archive_index'),

urlpatterns += patterns('weblog.views',
    (r'^tag/(?P<tag>\w+)/$', 'tag'),

10.  include other url

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^weblog/',        include('')),
    (r'^documentation/', include('')),
    (r'^comments/',      include('django.contrib.comments.urls')),

11. passing extra param

urlpatterns = patterns('blog.views',
    (r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}), #year = "2008", foo = "bar"

12. to be continued….


, ,

  1. #1 by Aruj on January 13, 2009 - 8:43 am

    Regular Expression!!!!!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: