Django URL syntax
January 13, 2009
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….
January 13, 2009 at 8:43 am
Regular Expression!!!!!!!
May 7, 2011 at 11:26 pm
Y66UHN http://gdjI3b7VaWpU1m0dGpvjRrcu9Fk.com
August 1, 2011 at 3:03 am
i’m fine good work nude lolitas 12 14 8-[
September 6, 2011 at 6:19 pm
Can I use your phone? underagemasturbating qcjgkk
September 8, 2011 at 7:44 am
Just over two years preteez toplist
8)))
September 21, 2011 at 8:19 am
Enter your PIN Sandra Img Bbs
P
September 22, 2011 at 6:13 am
On another call Underage Boy
September 25, 2011 at 1:31 pm
Could you ask him to call me? Nn Child Model hivdu
September 26, 2011 at 4:18 am
I’m training to be an engineer Lolita Sex ugdk
May 5, 2012 at 1:10 pm
How do you spell that? http://aropilyjyumi.de.tl dollysupermodel She is perfect. I would love for her to grind on me. Love her pussy. Too bad the photography was bad.