Conversion of Django URL tags

Source: Internet
Author: User
Use of Django URL tagsWhen developing applications with Django at the beginning of the year, it was completely in URLs. configure the IP address in Py hard encoding, in views. in py, httpresponseredirect () is also a hard-coded redirection address, and of course it is the same in template. This causes a problem if. in py, the address of a page is modified. in both Py and template. If it is a large project, there will be a lot of changes. Of course, you may choose a tool to directly find and replace it. In addition .....

In fact, Django itself provides this function, that is, the URL tag, which uses the Django URL tag to implement this function. In this module: Django/CONF/URLs/defaults

After the URL tag is used, no matter how the name of an address in urlpatterns changes, the addresses in templates do not need to be modified. When calling the URL tag in the template, You Need To: {% load URL from future %}

For example, if the URL function is not used:
Urlpatterns defines the home address of the information,
Program code
Urlpatterns = patterns ('',
(R' ^ Article $ ', 'news _ Index '),
)
The HTML in templates is
<A href = "/article"> Information </a>
There may be more than one page, and many pages may use information links. In this case, there will be many page a tags on your templates. One day, you suddenly want to change the name of the address,
Program code
Urlpatterns = patterns ('',
(R' ^ news $ ', 'news _ Index '),
)
In templates, you can modify 10 <a href = "/article"> Information </a> to <a href = "/News"> Information </a>.
What is hateful is that tags are distributed across different pages. Even worse, you don't know how many a tags are there (they cannot be counted in numbers ).

The URL is quite different,
Urlpatterns defines the home address of the information,
Program code
Urlpatterns = patterns ('',
URL (R' ^ Article $ ', 'news _ Index', name = "news_index "),
)
The HTML in templates is
Program code
<A href = "{% URL 'news _ Index' %}"> Information </a>

How do you modify the URL patterns address? The template changes, saving you a lot of trouble.

URL Usage is also very simple, as long as you use it in urlpatterns, append a name, such:
Program code
URL (R' ^ Article $ ', 'news _ Index', name = "news_index "),
Use this in templates
Program code
{% URL 'name' %}
The URL can be used. Note that the name is global, and you can only have one unique name in the entire urlpatterns. This principle should be well understood, just as the website address is also unique.
Templates is easy to use. How to use it in views? When a URL function is not used in the past, it may point to an address
Httpresponseredirect ("/article ")
Of course, when urlpatterns changes the address name, the parameters of the Views point to the function must be changed. With the URL function, it becomes:
Httpresponseredirect (reverse ("news_index "))
The benefits are the same as those used in the template.

When the URL patterns address contains parameters, such:
Program code
(R' ^ (? P <year> \ D {4 })/(? P <month> \ D {1, 2})/$ ', 'news _ list '),
There are two parameters, the final address such as the archive address http://www.yihaomen.com/2010/02
The situation becomes more complex. The usage of urlpatterns remains unchanged:
Program code
URL (R' ^ (? P <year> \ D {4 })/(? P <month> \ D {1, 2})/$ ', 'news _ list', name = "news_archive "),

The usage in templates needs to be changed. We regard the URL as a method, and combine the templates syntax, the result is:
Program code
<A href = "{% URL 'news _ archive' 2010 02%}"> August 1, February 2010 </a>
Or:
<A href = "{% URL 'news _ archive' year = 2010 month = 02%}"> August 1, February 2010 </a>

Of course, the methods in your backend views. py must also have these two parameters, for example
Program code
Def news_list (request, year, month ):
Print 'year: ', year
Print 'monty: ', month
......

The values 2010 and 02 are parameters. They are separated by commas. The usage of these parameters is the same. Of course, the 2010 02 parameter is obtained by an object. The specific situation is analyzed in detail. In Views, how can we write parameters:
Program code
From Django. Core. urlresolvers import reverse
......
Reverse ("news_archive", kwargs = {"year": 2010, "month": 02 })
For example: Return httpresponseredirect (reverse ("news_archive", kwargs = {"year": 2010, "month": 02 }))
The resolved address is "/2010/02 ".

From this we can see that when using Django to develop applications, URL tags are flexible and should be used in multiple ways to facilitate future maintenance. Reprinted from: one door light dance fat goat http://www.yihaomen.com/article/python/355.htm
 

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.