Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Django url () function Detailed     - Ubuntu 14.04 set auto sleep time (Linux)

- Linux system started to learn: how to view the contents of the seed on the Linux file (Linux)

- Ubuntu Linux installation GAMIT10.6 (Linux)

- VPS xen openvz kvm (Server)

- Upgrading to MySQL 5.7 partitioning problem solving (Database)

- About AWR More Description (Database)

- MySQL combination Git achieve differential backups can be used in the production environment (Database)

- The Linux firewall is configured to use proxy (Linux)

- Tmux create the perfect terminal management tool under CentOS (Linux)

- CentOS 7 hard disk expansion toss record (Linux)

- iOS developers - a simple factory pattern and OC reflection (Programming)

- How to fix apt-get update can not add a new CD-ROM error (Linux)

- Debian (Wheezy) were installed wxPython GUI development (Linux)

- Linux system find command Detailed (Linux)

- Spring multi data source configuration (Programming)

- Oracle multi-table query optimization (Database)

- Oracle online redefinition (Database)

- 256 with rich colors decorate your terminal (Linux)

- SecureCRT 7.0 Log Ubuntu 12.04 server via SSH service under Vmware (Server)

- SecureCRT remote connection Ubuntu fails to solve the case (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:      
 
- DRBD installation configuration, working principle and Recovery (Server)
- Linux --- manual release system cache (Linux)
- Java static internal class (Programming)
- Linux install and configure Heartbeat (Server)
- Implement Oracle dynamic registration of non-standard port 1521 (Database)
- Linux iostat command example explanation (Linux)
- Linux C source code (sockettype: socket descriptor determination function) (Programming)
- Installation Mate Desktop in FreeBSD 10.1 (Linux)
- Linux environment variables inside (Linux)
- Linux Shell Scripting (Programming)
- C # DateTime structure common method (Programming)
- How to compile and install wxWidgets in Ubuntu / Debian / Linux Mint (Linux)
- Use top to monitor the remote server (Server)
- Linux system performance tuning of Analysis (Linux)
- Implicit conversion from Java type conversion compare MySQL and Oracle (Database)
- Use Nginx as a load balancer (Server)
- Java reflection by calling the class method (Programming)
- Use CutyCapt to convert HTML pages to png images on Linux (Linux)
- Win7 + Ubuntu Kylin + CentOS 6.5 installed three systems (Linux)
- HTTPS and SSH and use the difference between the way: Git User's Manual (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.