Changes between Version 1 and Version 2 of CookBookMakeURLConfDRY


Ignore:
Timestamp:
05/02/06 10:22:54 (9 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookMakeURLConfDRY

    v1 v2  
    1 
    21== Question: ==
    32
    43In a url configuration file, we use the first argument to the patterns() function to specify a prefix to apply to each view function , avoiding typing that out for each entry in urlpatterns. but in the practical application, it is quite normal that only part of patterns have the common prefix. In that case, we cann't use a prefix directly for example:
    54
     5
     6{{{
    67urlpatterns = patterns( ''
    78             (r'^/?$', 'django.views.generic.date_based.archive_index'),
     
    910             (r'^tag/(?P<tag>\w+)/$', 'weblog.views.tag'),
    1011              )
     12}}}
     13
    1114
    1215== Answer ==
     
    1417All Django URL resolver needs is a urlpatterns variable in the url configuration, which is a list of pattern objects returned by the patterns() function. So we can do as the following to make our configuration DRY:
    1518
     19
     20{{{
    1621urlpatterns = patterns( 'django.views.generic.date_based'
    1722             (r'^/?$', 'archive_index'),
     
    2227             (r'^tag/(?P<tag>\w+)/$', 'tag'),   
    2328              )
     29}}}
    2430
     31
Back to Top