Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: formats of urlpatterns in urls.py

Tags:

url

django

I noticed that in Django there are two formats of urlpatterns in file urls.py:

urlpatterns = [
    url(...),
    url(...),
]

and

urlpatterns = pattern('',
    url(...),
    url(...),
)

The first is a list of url instances, and the second invokes the pattern module with an empty string and a number of url instances as parameters.

  1. What is the difference between the two?
  2. What is the purpose of an empty string in the second format?
  3. Which one is recommended to use?
like image 539
Randy Tang Avatar asked Jul 17 '15 10:07

Randy Tang


People also ask

What is url patterns in Django?

In Django, views are Python functions which take a URL request as parameter and return an HTTP response or throw an exception like 404. Each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration) Let the project name be myProject.

What is name in Django urls?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .

How does Django define dynamic url?

You can use named groups in the urls to pass data to views and it won't require any dynamic updating in the urls. The named part containing page. alias will be simply passed as a keyword argument to your view function. You can use it to get the actual Page object.

Which method is used instead of path () in urls py?

If the paths and converters syntax isn't sufficient for defining your URL patterns, you can also use regular expressions. To do so, use re_path() instead of path() . In Python regular expressions, the syntax for named regular expression groups is (?


2 Answers

In Django 1.8+, urlpatterns should simply be a list of url()s. This new syntax actually works in 1.7 as well.

urlpatterns = [
    url(...),
    url(...),
]

The old syntax using pattern is deprecated in Django 1.8, and is removed in Django 1.10.

urlpatterns = pattern('',
    url(...),
    url(...),
)

With the old syntax, you could provide a prefix. The example given in the docs is

urlpatterns = patterns('news.views',
    url(r'^articles/([0-9]{4})/$', 'year_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'month_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'article_detail'),
)

However, using strings arguments for the view is now deprecated as well, and you should provide the callable instead.

like image 188
Alasdair Avatar answered Oct 19 '22 11:10

Alasdair


Per the documentation, patterns is:

A function that takes a prefix, and an arbitrary number of URL patterns, and returns a list of URL patterns in the format Django needs.

The first argument to patterns() is a string prefix.

It also provides an example of why you might want to use it:

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),
)

In this example, each view has a common prefix – 'news.views'. Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.

With this in mind, the above example can be written more concisely as:

from django.conf.urls import patterns, url

urlpatterns = patterns('news.views',
    url(r'^articles/([0-9]{4})/$', 'year_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'month_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'article_detail'),
)

However, note that this function is deprecated:

Deprecated since version 1.8:

urlpatterns should be a plain list of django.conf.urls.url() instances instead.

Note that the explanation as to why includes (with good reason, clearly!):

Thus patterns() serves little purpose and is a burden when teaching new users (answering the newbie’s question "why do I need this empty string as the first argument to patterns()?").

like image 44
jonrsharpe Avatar answered Oct 19 '22 10:10

jonrsharpe