Django Getting Started: Django URLs configuration

Source: Internet
Author: User
Tags regular expression

When the browser requests a URL address from the Web server, the patterns method in the urls.py file responds, and the response is matched by a URL method, which requires 4 parameters to be passed in the matching process, and the function of the 4 parameters is as follows:
URL (path, view, other, name)
path: A path string match using the Regex method
view: is defined by the function in the views under the app
Other: can pass any parameter to the target view
Name: You can pass the parameters here to a special templates (templates later, which is the Django-specific programming pattern--MVT,MVT programming model needs to use, Templates is specifically responsible for the display of the Web front end)

e.g:
#app/urls.py from
django.conf.urls import patterns, include, url from
django.contrib import admin

urlpatterns = Patterns (",
    # Examples:
    # URL (r ' ^$ ', ' models_foreign.views.home ', name= ' home '),
    # URL (r ' ^blog/', include (' Blog.urls ')),

    URL (r ' ^admin/', include (Admin.site.urls)),
    URL (r ' ^$ ', Views.index , name= ' index '),
)

Official citation:
The URL () function is passed four arguments, and the other required: regex and view, and the optional: kwargs< /c2>, and name. At the this point, it's worth reviewing what these arguments is for. URL () Argument:regex

The term "regex" was a commonly used short form meaning "regular expression", which are a syntax for matching patterns in St Rings, or in the This case, URL patterns. Django starts at the first regular expression and makes it's to the list, comparing the requested URL against each re Gular expression until it finds one that matches.

Note that these regular expressions does not search GET and POST parameters, or the domain name. For example, with a request to https://www.example.com/myapp/, the URLconf would look for myapp/. In a request to https://www.example.com/myapp/?page=3, the URLconf would also look for myapp/.

If you need help with regular expressions, see Wikipedia's entry and the documentation of the RE module. Also, the O ' Reilly book "Mastering Regular Expressions" was Jeffrey by Friedl fantastic. In practice, however, you don't need to being an expert on regular expressions, as-you-really only need-know how to Captur e simple patterns. In fact, complex regexes can has poor lookup performance, so we probably shouldn ' t rely on the full power of regexes.

Finally, a performance Note:these regular expressions is compiled the first time of the URLconf module is loaded. They ' re super fast (as long as the lookups aren ' t too complex as noted above). URL () Argument:view

When Django finds a regular expression match, Django calls the specified view function, with an HttpRequest object as the First argument and any "captured" values from the regular expression as other arguments. If the regex uses simple captures, values is passed as positional arguments; If it uses named captures, values are passed as keyword arguments. We ' ll give an example of the this in a bit. URL () Argument:kwargs

Arbitrary keyword arguments can be passed on a dictionary to the target view. We aren ' t going to use this feature for the Django in the tutorial. URL () argument:name

Naming your URL lets you refer to it unambiguously from elsewhere in Django especially templates. This powerful feature allows your make global changes to the URL patterns of your project and only touching a single F Ile.

When you ' re comfortable with the basic request and response flow, read Part 2 of this tutorial to start working with th E database.

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.