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.
- What is the difference between the two?
- What is the purpose of an empty string in the second format?
- Which one is recommended to use?
Per the documentation,
patterns
is:It also provides an example of why you might want to use it:
However, note that this function is deprecated:
Note that the explanation as to why includes (with good reason, clearly!):
In Django 1.8+, urlpatterns should simply be a list of
url()
s. This new syntax actually works in 1.7 as well.The old syntax using
pattern
is deprecated in Django 1.8, and is removed in Django 1.10.With the old syntax, you could provide a prefix. The example given in the docs is
However, using strings arguments for the view is now deprecated as well, and you should provide the callable instead.