Django URL Soft-coded

Source: Internet
Author: User

At the beginning of the development of the application with Django, is completely in the urls.py in the hard-coded configuration address, in views.py Httpresponseredirect () is also hard-coded to the address, of course, the same in the template, which brings a problem, If you modify the address of a page in urls.py, all the places (views.py and template) are modified. If it's a big project, there are a lot of places to change. Of course, you might choose a tool that directly looks for replacements to implement. Besides that .....

In fact, Django itself provides this functionality, URL tags, using Django URL tags to implement such a function, in this module: django/conf/urls/defaults

After using the URL tag, no matter how the address in the urlpatterns is changed, the address in the templates does not have to be modified. When calling the URL tag in the template, you need to: {% load URL from the future%}

For example, when a URL function is not used:
Urlpatterns defines the home address of the information,

Program code
Urlpatterns = Patterns ("',
(R ' ^article$ ', ' News_index '),
)


The HTML in the templates is
<a href= "/article" > News </a>
And more than one page, there may be a lot of pages to use the information link, then you will have a lot of templates on the page a tag, when one day, you suddenly want to change the address of the term,

Program code
Urlpatterns = Patterns ("',
(R ' ^news$ ', ' News_index '),
)


You will find that you have to modify the templates 10 <a href= "/article" > Information </a> <a href= "/news" > Information </a>
The hateful thing is that the labels are distributed on different pages, and even worse, you don't know how many such a tags there are (not a single number).

With the URL, the situation is greatly different.
Urlpatterns defines the home address of the information,

Program code
Urlpatterns = Patterns ("',
URL (r ' ^article$ ', ' News_index ', name= "News_index"),
)


The HTML in the templates is

Program code
<a href= "{%url ' news_index '%}" > news </a>



How do you change the address of the Urlpatterns, template will change with a lot of trouble.

The use of the URL is also simple, as long as it is used in Urlpatterns, attach a name, such as:

Program code
URL (r ' ^article$ ', ' News_index ', name= "News_index"),


This is used in templates

Program code
{%url ' name '%}


The address link can be used. Note that the name is global, you can only have a unique name in the whole urlpatterns, this principle should be well understood, as the site's address is also unique.
Templates in the use of simple, in the views of how to use it? Previously, when no URL function was used, it might point to an address using the
Httpresponseredirect ("/article")
Of course, when changing the address of the urlpatterns, the arguments used for the point function of the views are changed accordingly. With the URL function, it becomes:
Httpresponseredirect (Reverse ("News_index"))
The benefits are the same as those used in the template.

When encountering a urlpatterns address that contains parameters, such as:

Program code
(R ' ^ (? P&LT;YEAR&GT;\D{4})/(? p<month>\d{1,2})/$ ', ' news_list '),


There are two parameters, the final address such as the archived address http://www.yihaomen.com/2010/02
The situation becomes more complicated, and the usage of urlpatterns is the same:

Program code
URL (r ' ^ (? P&LT;YEAR&GT;\D{4})/(? p<month>\d{1,2})/$ ', ' news_list ', name= "news_archive"),



The use of templates needs to be changed, we regard the URL as a method, combined with the templates syntax, the results come out:

Program code
<a href= "{%url ' news_archive ' 02%}" >2010 year February </a>
or this:
<a href= "{%url ' news_archive ' year=2010 month=02%}" >2010 year February </a>



Of course, you must also have these two parameters in the views.py method in your background, such as

Program code
def news_list (Request,year,month):
print ' Year: ', year
print ' Monty: ', Month
......



The following 2010, 02 is the parameter, the parameters are separated by commas, how many parameters are used the same. Of course, the 2010 02 parameter is obtained by an entity, and the specific case is analyzed. And in the views, with the parameters how to write, million change not to leave the Pope:

Program code
From django.core.urlresolvers Import Reverse
......
Reverse ("news_archive", kwargs={"year":, "month": 02})


For example: Return Httpresponseredirect (Reverse ("news_archive", kwargs={"year":, "month": 02}))
Then, the last resolved address is "/2010/02".

Thus, when using Django to develop the application, the URL tag is a very flexible thing, should be more useful, but also for later maintenance to bring convenience.

Unless stated, the articles are One DoorOriginal, reproduced please specify the address of this article, thank you! [This diary is edited by 2013-01-23 12:23

Django URL Soft-coded

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.