A detailed description of the Django URL () function

Source: Internet
Author: User

The URL () function looks like this: url(r^/account/$‘, views.index, name=index) it can receive four parameters, which are two required parameters:, regex view and two optional parameters:, and kwargs name then the four parameters are described in detail.

Regex

The regex represents a regular expression, and any URL request that matches the Regex executes view in the view function represented by the second parameter in the URL () function. Note that the regular expression does not match the domain name and query parameters in the URL, such as: Http://www.foofish.net/article/?page=3, Django is only looking for article/ . The regular expression is compiled when the urlconf module is loaded, so the speed is fast at the time of the match.

View

After the Django match regular expression succeeds, the corresponding view function is found, and Django always passes the HttpRequest object as the first argument to the view function, and the parameter that is carried in the Regex parameter is passed as an optional argument to the view function. For example:, the url(r‘^(?P<article_id>\d+)/$‘, views.detail, name=‘detail‘) (?P<article_id>\d+) parameters inside the parentheses are passed as the second argument to the View function detail(request, article_id) , where the names of the arguments must be identical. Because you specify the name of the parameter in the URL function, you can also not display the specified, such as: url(r‘^(\d+)/$‘, views.detail, name=‘detail‘) , so in the view function, the name of the second parameter is casually named. It is matched according to the position of the positional parameter.

Name before name, let's talk about the built-in tag URL of the Django template. {% url path.to.some_view%}You can return the URL of the view function (relative to the absolute path of the domain name), such as url(r^/account/$‘, views.index, name=index)Use {% url view.index %}will return /accout/, this can improve the flexibility of the template, if it is hard-coded way, the template is difficult to maintain.

You may encounter a problem when using the tag URL: for:

1Urlpatterns = Patterns ("',2URL (r'^archive/(\d{4})/$', archive, Name="full-archive"),3URL (r'^archive-summary/(\d{4})/$', archive, {'Summary': True},"arch-summary"),4)

The same view function has multiple urlconf, and when the template system wants to get the URL through the view name, archive It is overwhelmed, and the name parameter is used to solve the problem. The name is used for a single-area view that corresponds to multiple urlconf scenes. Gets the URL in reverse by name.
Such as:

1Urlpatterns = Patterns ("',2URL (r'^archive/(\d{4})/$', archive, Name="full-archive"),3URL (r'^archive-summary/(\d{4})/$', archive, {'Summary': True},"arch-summary"),4)

In the template you can use:

1 {% URL arch-summary 1945%}

2 {% URL full-archive}

Kwargs

Kwargs is a dictionary-type parameter that is used in such a way as:

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

The Kwargs here is{‘summary‘: True}

This is how the view function is used:

def Archive (request, archive_id, summary):

Attention:

  1. If there is a configuration in url.py, if there url(r‘^comment/(\d{1,9})/delete/$‘,‘delete_comment‘), is no delete_comment such a function view, if the tag is used in the template {% url path.to.some_view %} , then the Viewdoesnotexit error is thrown. Think it makes sense, if the view does not exist, even if it matches the URL, when accessing the URL, will still throw Viewdoesnotexit exception, here Django just load parse urlconf when the check.
  2. If you used the url.py file in the rooturl(r‘^people/‘, include(‘people.urls‘, namespace=‘people‘)), here people is an app, then in people this app in the url.pyurl(r‘^(\d{1,9})/$‘,‘index‘, name=‘index‘)You must specify name=index to use {% url ' people:index '%} properly, otherwise:
     1  noreversematch at/2  Reverse for   " subjects   " with arguments "   ()  "  and  keyword Arguments  " {}   '  not  found 

    Of course, if you are sure that this is not the exception 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 references
    https://docs.djangoproject.com/en/1.1/topics/http/urls/#id2
    https://docs.djangoproject.com/en/1.1/ref/templates/builtins/#std: Templatetag-url

A detailed description of the Django URL () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.