Milk
Milk

Reputation: 2655

Blank URL pattern in Django

I've inherited a Django app and have noticed urlpatterns += patterns('') and equivalent throughout urls.py.

e.g.

urlpatterns = patterns(
    '', 
    url(r'^index.html', render_index),
)

#... 

urlpatterns += patterns(
    '', 
    url(r'^page.html', another_controller),
)

What is this doing? Anything?

Upvotes: 2

Views: 1336

Answers (1)

rgilligan
rgilligan

Reputation: 804

It's needed in the patterns() function because the first argument to patterns() is used as a common view prefix to your urls. From the docs:

urlpatterns = patterns('',
    (r'^articles/(\d{4})/$', 'news.views.year_archive'),
    (r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
    (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)

Is written more simply as:

urlpatterns = patterns('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'),
)

However, since Django 1.8, the urlpatterns variable in urls.py is created with a simple list:

urlpatterns = [ 
    url(r'^index.html', render_index),
    url(r'^page.html', another_controller),
]

and this view prefix argument isn't needed.

Upvotes: 2

Related Questions