Detailed explanation of Django url() function

The format of the url() function looks like: url(r^/account/$', views.index, name=index), it can receive four parameters, which are two mandatory parameters: regex, viewand two optional parameters: kwargs, name. Next, we will introduce these four parameters in detail.

regex

Regex represents a regular expression, and any URL request that matches regex will be executed to viewthe view function represented by the corresponding second parameter in the url() function. It should be noted that the regular expression will not match the domain name and query parameters in the URL, such as: http://www.foofish.net/article/?page=3, Django only finds article/. Regular expressions are compiled when the URLconf module is loaded, so matching is fast.

view

After Django matches the regular expression successfully, it will find the corresponding view function. Django always passes the HttpRequest object as the first parameter to the view function, and uses the parameters carried in the regex parameter as optional parameters to pass to the view function. For example: url(r'^(?P<article_id>\d+)/$', views.detail, name='detail'),, (?P<article_id>\d+)the parameters in the parentheses will be passed to the view function as the second parameter detail(request, article_id), where the names of the parameters must be exactly the same. Because you explicitly specified the name of the parameter in the url function, of course, you can also specify it without display, such as: url(r'^(\d+)/$', views.detail, name='detail'), so that in the view function, the name of the second parameter can be named arbitrarily. It matches based on the position of the positional argument.

name
Before talking about name, let’s talk about the built-in tag url of Django template, which {% url path.to.some_view%}can return the URL corresponding to the view function (the absolute path of the relative domain name). For example url(r^/account/$', views.index, name=index), using {% url view.index %}will return /accout/, which can improve the flexibility of the template. If it is hard-coded In this way, templates are difficult to maintain.

A problem you may encounter when using tag urls is: For:

1 urlpatterns = patterns('',
2     url(r'^archive/(\d{4})/$', archive, name="full-archive"),
3     url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"),
4 )

The same view function has multiple urlconfs. At this time, the template system archiveis at a loss when it wants to obtain the URL through the view name. The name parameter is used to solve this problem. name is used to uniquely distinguish a view corresponding to multiple urlconf scenarios. Get the URL in reverse by name.
Such as:

1 urlpatterns = patterns('',
2     url(r'^archive/(\d{4})/$', archive, name="full-archive"),
3     url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"),
4 )

In templates you can use:

 1 {% url arch-summary 1945 %}

2 {% url full-archive 2007 %} 

kwargs

kwargs is a dictionary type parameter, which is used as follows:

url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"),

The kwargs here are{'summary': True}

This is how it is used in the view function:

def archive(request, archive_id, summary):

Notice:

  1. If url(r'^comment/(\d{1,9})/delete/$','delete_comment'),there , if there is no delete_commentsuch function view, if {% url path.to.some_view %}this tag is used in the template, then throw ViewDoesNotExit error. If you think about it carefully, it makes sense. If the view does not exist, even if the URL is matched, the ViewDoesNotExit exception will be thrown when the URL is accessed. Here, Django only checks when the URLConf is loaded and parsed.
  2. If it is used in the root url.py file url(r'^people/', include('people.urls', namespace='people')), where people is an app, then url(r'^(\d{1,9})/$','index', name='index')name=index must be specified in the url.py in the people app to use {% url 'people:index'%}, otherwise:
    1 NoReverseMatch at /
    2 Reverse for 'subjects' with arguments '()' and keyword arguments '{}' not found

    Of course, if you are sure that this exception is not thrown by the above question, then you can look at these two answers:
    http://stackoverflow.com/questions/9649587/reverse-for-with-arguments-and-keyword-arguments-not- found
    http://stackoverflow.com/questions/14882491/django-release-1-5-url-requires-a-non-empty-first-argument-the-syntax-change
    This article refers to
    https://docs.djangoproject. com/en/1.1/topics/http/urls/#id2
    https://docs.djangoproject.com/en/1.1/ref/templates/builtins/#std:templatetag-url

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066778&siteId=291194637