Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Django url () function Detailed     - How to create an alternative Android / iOS connected wireless hotspot AP in Ubuntu 15.04 (Linux)

- The sublime into IDE (Linux)

- Workaround CentOS error message during compilation PHP5 common (Linux)

- To compile and install OpenCV-2.3.1 FFmpeg-2.1.2 under CentOS (Linux)

- Dynamic programming Android (Programming)

- Linux User Management (Linux)

- Job achieve automation in Ubuntu 14.04 - Using Cron (Linux)

- Use Vagrant build cross-platform development environment for Python (Server)

- Digital jQuery scrolling effect (Programming)

- Java in the inverter and covariance (Programming)

- CentOS 7.1 install NTFS-3G (Linux)

- Linux system - The understanding cpu load (Linux)

- Using DBMS_STAT function closes mission (Database)

- Android timer use (Programming)

- Ubuntu Apache virtual host configuration (Server)

- Use PuTTY to access the virtual machine Linux under Windows xp (Programming)

- Oracle create a temporary table space group (Database)

- Joseph Central Java implementation (Programming)

- Programmer editor Vim (Linux)

- How to turn Java String into Date (Programming)

  Django url () function Detailed
  Add Date : 2017-04-13      
  Url () function looks like the format: url (r ^ / account / $ ', views.index, name = index), it can receive four parameters, namely the two mandatory parameters: regex, view and two optional parameters: kwargs, name, followed by a detailed description of these four parameters.


regex on behalf of a regular expression, all with regex matching URL requests are executed to url () function corresponding to the second parameter representative of the view the view function. Note: The regular expression does not match the domain name in the URL and query parameters, such as: http: //www.foofish.net/article/ page = 3, Django only to find article /?. Regular expressions URLconf module is loaded when it is compiled, so the match when the speed is very fast.


After Django regular expression matching is successful, you will find the appropriate view function, Django always passed to the view function with HttpRequest object as the first parameter, in addition to using regex parameter carried argument as an optional parameter passed to the view function. Such as: url (r '? ^ (P < article_id> \ d +) / $', views.detail, name = 'detail') ,, parentheses (? P < article_id> \ d +) inside the second parameter argument to the view function detail (request, article_id), where the parameter name must be exactly the same. Because you appear in the url function specifies the name of the argument, of course, you can not specify a display, such as: url (r '^ (\ d +) / $', views.detail, name = 'detail'), in view of this function, the name of the second parameter casually named. It is according to the position to match the position of the parameter.


Speaking before the name, let us talk about the built-in Django template tag url, {% url path.to.some_view%} function returns the view corresponding URL (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 /, doing so can increase the flexibility of the template, if you are using hard-coded, stencil difficult to maintain.

Use labels url, they might have a problem is this: For:

urlpatterns = patterns ( '',
    url (r '^ archive / (\ d {4}) / $', archive, name = "full-archive"),
    url (r '^ archive-summary / (\ d {4}) / $', archive, { 'summary': True}, "arch-summary"),
With a view function has multiple urlconf, template system at this time want to get the URL when the archive through the view name, you know what to do, name parameter is used to solve this problem. The only name to view a region corresponding to a plurality of urlconf scene. Get URL to reverse by name.
Such as:

urlpatterns = patterns ( '',
    url (r '^ archive / (\ d {4}) / $', archive, name = "full-archive"),
    url (r '^ archive-summary / (\ d {4}) / $', archive, { 'summary': True}, "arch-summary"),
Can be used in the template:

{% Url arch-summary 1945%}
{% Url full-archive 2007%}

kwargs is a dictionary type of parameters, such as how it is used:

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

View function is used like this:

def archive (request, archive_id, summary):

If there url (r '^ comment / (\ d {1,9}) / delete / $', 'delete_comment') in url.py the configuration, if there is no such a function delete_comment view, if the template use the {% url path.to.some_view%} this tag, then throw ViewDoesNotExit error. Think about it makes sense, if the view does not exist, even if the match to the URL, this URL when accessing the time, or will throw ViewDoesNotExit exception, where Django just loaded resolve URLConf when it was examined.
If the url in the root url.py file (r '^ people /', include ( 'people.urls', namespace = 'people')), where people is a app, then the url in the people of this app. py in the url (r '^ (\ d {1,9}) / $', 'index', name = 'index') must specify the name = index to normal use {% url 'people: index'%}, otherwise:

NoReverseMatch at /
Reverse for 'subjects' with arguments '()' and keyword arguments '{}' not found
Of course, if you are determined not to be the above-mentioned problems of this exception is thrown, you can look at these two answers:
Herein by reference
- CentOS 5.3 under broadcom NIC dual activation issues (Linux)
- Linux directory configuration (Linux)
- exp / imp Export Import version of the problem and the ORA-6550 error (Database)
- iOS GCD multithreading simple to use (Programming)
- RPM package creation Getting Started (Linux)
- Virtualbox virtual machine can not copy CentOS Network (Linux)
- 25 Git Advanced Skills (Linux)
- After CentOS configure SSH password Free, still prompted for a password (Linux)
- Various sorting algorithms implemented in Python (Programming)
- Struts2 study notes -Valuestack (value stack) and OGNL expression (Programming)
- Iptables application layer plug (Linux)
- Php and MySQL command add to the environment variable method in Linux system (Linux)
- Spring Data MongoDB combat (Database)
- MySQL 5.6.12 binary log path switching binlog (Database)
- After installation of Debian 6.0 do a few things first (Linux)
- Configuring Haproxy log support (syslog logging support) (Server)
- shellinabox: one uses AJAX Web-based terminal emulator (Linux)
- grep regular expression (Linux)
- Depth understanding of JavaScript new mechanism (Programming)
- Ubuntu resolve sudo: source: command not found error (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.