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.
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.
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 .
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.
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 (?
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.
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 stringprefix
.
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 inurlpatterns
, you can use the first argument to thepatterns()
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 ofdjango.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 topatterns()
?").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With