Bill Noble
Bill Noble

Reputation: 6734

What is significance of omitting 'url(' in Django urlpatterns

I have been given some Django code to take over where some of the entries in the urlpatterns in the settings.py use url() and some don't as can be seen here:

urlpatterns = patterns('',
    (r'^grappelli/', include('grappelli.urls')), 
    url(r'^admin/', include(admin.site.urls)),
    (r'^movies/', include('movies.urls')),
    (r'^profile/', include('userprofile.urls')),
    (r'^api/', include(v1_api.urls)),
    url(r'^$', TemplateView.as_view(template_name='base.html'), name='home'),
    url(r'^trailer/', TemplateView.as_view(template_name='trailer.html'), name='trailer'),
    url(r'^newuser/', TemplateView.as_view(template_name='trailer.html'), name='activate'), 
    url(r'^abcd123/', TemplateView.as_view(template_name='trailer.html'), name='url_login'), 
    url(r'^fb/', TemplateView.as_view(template_name='fb.html'), name='fb'), 
)

Does it make any difference whether or not url( is used? Is there a recommended standard?

Upvotes: 2

Views: 103

Answers (3)

FlipperPA
FlipperPA

Reputation: 14331

Here's a full example of how you'd do it in Django 1.8 (and forward), using a list of tuples, as mentioned. 1.8 is a long-term release, so upgrading to 1.8 will guarantee you security releases for at least three years.

urlpatterns = [
    url(r'^grappelli/', include('grappelli.urls')), 
    url(r'^admin/', include(admin.site.urls)),
    url(r'^movies/', include('movies.urls')),
    url(r'^profile/', include('userprofile.urls')),
    url(r'^api/', include(v1_api.urls)),
    url(r'^$', TemplateView.as_view(template_name='base.html'), name='home'),
    url(r'^trailer/', TemplateView.as_view(template_name='trailer.html'), name='trailer'),
    url(r'^newuser/', TemplateView.as_view(template_name='trailer.html'), name='activate'), 
    url(r'^abcd123/', TemplateView.as_view(template_name='trailer.html'), name='url_login'), 
    url(r'^fb/', TemplateView.as_view(template_name='fb.html'), name='fb'), 
]

You can also remove patterns from your import list at that time.

Upvotes: 5

Andrea Corbellini
Andrea Corbellini

Reputation: 17761

Tuples are the old (deprecated) way for specifying url patterns.

In your case, with the version of Django you are using, it does not make any difference if you use tuples or url()s.

Also note that, since Django 1.8, the call to patterns() is no longer required and has been deprecated. Django 1.8 recommends using a list of url()s instead.

Upvotes: 2

dhke
dhke

Reputation: 15398

Does it make any difference whether or not url( is used?

No. Passing a list or a tuple is treated in the same way as if the arguments where wrapped in url():

django/urls/__init__.py:

 if isinstance(t, (list, tuple)):                                      
     t = url(prefix=prefix, *t)
 elif isinstance(t, RegexURLPattern):
     t.add_prefix(prefix)

Is there a recommended standard?

Use url().

config.urls.patterns() itself is scheduled for deprecation in django 1.10, at which point, urlpatterns must be a list of url() instances and raw tuples cannot be used anymore.

Also: explicit is better than implicit.

Upvotes: 3

Related Questions