Buttons840
Buttons840

Reputation: 9637

Why is the url function used more often in Django 1.5 documentation?

I'm working with some older Django code and the url function is not used anywhere, similar to the examples in the Django 1.4 documentation:

from django.conf.urls import patterns, url, include

urlpatterns = patterns('',
    (r'^articles/2003/$', 'news.views.special_case_2003'),
    (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'),
)

but I notice in the Django 1.5 documentation the url function is used frequently:

from django.conf.urls import patterns, url

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

Why is this? Is it a matter of convention, or is there a technical reason to use the url function? Which practice should I follow in the future, and how should I maintain my legacy code without url calls?

Upvotes: 0

Views: 71

Answers (1)

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11048

From the docs

url(regex, view, kwargs=None, name=None, prefix='')

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 without the optional extra arguments dictionary. For example:

urlpatterns = patterns('',
    url(r'^index/$', index_view, name="main-view"),
    ...
)

And you use them for reverse URL matching (again, the docs)

You could convert the first example as:

url(r'^articles/2003/$', special_case_2003, name="special_case_2003"),

and call it in your template

{% url special_case_2003 %}

Yeah, maybe the two examples you posted are a bit too fuzzy about this

Upvotes: 2

Related Questions