Django URL syntax

Summarized from : http://docs.djangoproject.com/en/dev/topics/http/urls/?from=olddocs

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 http://www.example.com/myapp/, the URLconf will look for myapp/.

In a request to http://www.example.com/myapp/?page=3, the URLconf will look for myapp/.

The URLconf doesn’t look at the request method

4…..

  • 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/$', 'blog.views.page'),
    (r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
)

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

8.view prefix… make it shorter

urlpatterns = patterns(‘mysite.news.views’,
(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'),
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','archive_month'),
)

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('django_website.apps.blog.urls.blog')),
    (r'^documentation/', include('django_website.apps.docs.urls.docs')),
    (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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: