Use the naming group method in Django URLconf

Source: Internet
Author: User
This article mainly introduces how to use a naming group in Django's URLconf. Django is the most famous framework for various popular Pyhton development, if you need it, you can add parentheses to the URL section we want to capture. Django will pass the captured text to the view function as a location parameter. In more advanced usage, you can also use the named regular expression group to capture the URL and pass it as a keyword parameter to the view.

A Python function can be called using a keyword or location parameter. In some cases, it can be used at the same time. In the keyword parameter call, you must specify the parameter name and input value. In location parameter calls, you only need to input parameters, and you do not need to specify which parameter corresponds to which value. Their mappings are implicitly in the order of parameters.

For example, consider this simple function:

def sell(item, price, quantity):  print "Selling %s unit(s) of %s at %s" % (quantity, item, price)

To use the location parameter to call it, you must specify the parameter in the order defined by the function.

sell('Socks', '$2.50', 6)

To use a keyword parameter to call it, you must specify the parameter name and value. The following statements are equivalent:

sell(item='Socks', price='$2.50', quantity=6)sell(item='Socks', quantity=6, price='$2.50')sell(price='$2.50', item='Socks', quantity=6)sell(price='$2.50', quantity=6, item='Socks')sell(quantity=6, item='Socks', price='$2.50')sell(quantity=6, price='$2.50', item='Socks')

Finally, you can mix the keyword and location parameters as long as all the location parameters are listed before the keyword parameters. The following statement is equivalent to the previous example:

sell('Socks', '$2.50', quantity=6)sell('Socks', price='$2.50', quantity=6)sell('Socks', quantity=6, price='$2.50')

In Python regular expressions, the syntax of the named regular expression group is (? P Pattern), Here name is the group name, and pattern is a matching pattern.

The following is an example of using URLconf of an unknown group:

from django.conf.urls.defaults import *from mysite import viewsurlpatterns = patterns('',  (r'^articles/(\d{4})/$', views.year_archive),  (r'^articles/(\d{4})/(\d{2})/$', views.month_archive),)

The following is the same URLconf and the name group is rewritten:

from django.conf.urls.defaults import *from mysite import viewsurlpatterns = patterns('',  (r'^articles/(?P
 
  \d{4})/$', views.year_archive),  (r'^articles/(?P
  
   \d{4})/(?P
   
    \d{2})/$', views.month_archive),)
   
  
 

This code is exactly the same as the previous function. There is only one subtle difference: the value is passed to the view function in the form of a keyword parameter rather than a positional parameter.

For example, if a name group is not included, the request/articles/2006/03/will be equivalent to a function call like this:

month_archive(request, '2006', '03')

With a name group, the same request will become such a function call:

month_archive(request, year='2006', month='03')

Using a naming group can make URLconfs clearer, reduce potential bugs that confuse Parameter order, and help you sort parameters in function definitions. In the above example, if we want to modify the URL to put the month before the year, instead of using the name group, we have to modify the Parameter order of the view month_archive. If we use a naming group, modifying the order of extracted parameters in the URL does not affect the view.

Of course, the cost of naming groups is that they lose simplicity: Some developers think that the naming group syntax is ugly and redundant. Another benefit of naming groups is high readability.

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.