On Tuesday 26 May 2009 07:21:16 pm tekion wrote:
> Part 3 tutorial from django site, suggest the below method of
> optimizing urls.
> urlpatterns = patterns('mysite.polls.views',
>     (r'^polls/$', 'index'),
>     (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
>     (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
>     (r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
> )
>
> the key factor, is that you must have a common prefix. 

Yes.

> But what if 
> you do not have a common prefix, would it still work?  

Not with that single statement.  However, you can use this feature with 
different prefixes by making multiple calls to the patterns function.  Like 
this:

{{{
urlpatterns = patterns('mysite.polls.views',
    (r'^polls/$', 'index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)

urlpatterns += patterns('mysite.widgets.views',
    (r'^widgets/$', 'index'), 
   # etc.
)
}}}

Note that the first call says "urlpatterns = patterns", while the second 
says "urlpatterns += patterns".  This works because patterns is a function 
that returns a simple Python list.

If you do this, be careful that the first mention of urlpatterns is an 
assignment (=) and all subsequent mentions are appends (+=).  Otherwise 
you'll loose all the url patterns that preceded the final assignment... an 
error that can drive you nuts if you don't know to watch out for it.

-- 
Aaron Maxwell
http://redsymbol.net/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to