Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Django url () function Detailed     - Bash difference in single quotes and double quotes (Programming)

- Emacs install clojure development environment (Linux)

- Virtual Judge environment to build and configure under Ubuntu (Server)

- MySQL + Corosync + Pacemaker + DRBD build highly available MySQL (Server)

- C + + secondary pointer memory model (pointer array) (Programming)

- CentOS 5.10 installed Oracle 11G R2 (Database)

- Depth understanding of the use of Spring Redis (Programming)

- Xshell configure SSH free password (Server)

- Seven kinds of NIC binding mode Detail (Linux)

- Subsequent binary search tree traversal sequence (Programming)

- How to implement large-scale distributed Yahoo depth study on the Hadoop cluster (Server)

- Linux / BSD firewall M0n0wall Profile (Linux)

- Shell Scripting Basics (Linux)

- Two classic macro definition under Linux (Linux)

- How to configure security management services under Linux (Linux)

- Android development, may cause a memory leak problem (Programming)

- Linux system ARP attack solution (Linux)

- CentOS iptables firewall enabled (Linux)

- Oracle GoldenGate encryption (Database)

- Use in Linux ipmitool tool (Linux)

 
         
  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

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.

view

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.

name

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

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):
note:

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:
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
Herein by reference
https://docs.djangoproject.com/en/1.1/topics/http/urls/#id2
https://docs.djangoproject.com/en/1.1/ref/templates/builtins/#std:templatetag-url
     
         
       
         
  More:      
 
- Binary search -Java achieve (Programming)
- C language files update in real time (Programming)
- Java eight new features 8 (Programming)
- JavaScript: understanding regular expressions (Programming)
- Linux systems dmesg command processing failures and system information collected seven usage (Linux)
- DupeGuru- find and remove duplicate files (Linux)
- ctop: monitor container performance Linux command line artifact (Linux)
- Bash Automated Customization Linux belongs to its own CentOS system (Linux)
- MongoDB fragmentation (Cluster) (Database)
- Iptables on the request URL for IP access control (Linux)
- How do I upgrade from Ubuntu 15.04 to Ubuntu 15.10 (Linux)
- wget command Walkthrough (Linux)
- hadoop 2.7.1 High Availability Setup Deployment (Server)
- PL / SQL -> UTL_FILE use presentation package (Database)
- Linux disk management practices (Linux)
- Compare Several MySQL environmental issues (Database)
- Oracle 10g in the unique and index problems (Database)
- Linux-based Heartbeat high availability configuration httpd service (Server)
- Network security system (Network)
- Nginx DHCP TFTP Kickstart set up automatic installation system (Server)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.