The meaning of the name parameter of the Django URL

Source: Internet
Author: User

Templates's link address is based on the address defined by the Urlpatterns, pieced together into the address string, it is difficult to see, and the templates piece of the address, as the page increases and increase, once in the urlpatterns of an address changed the term, That tear is rushing, how many pieces of the address will have to change how many places! At this time found the URL function, this can be good, regardless of urlpatterns in the address of how to change, templates in the address are not modified.

For example, when a URL function is not used:

Urlpatterns defines the home address of the information,

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 10 pages to use the link to the information, then your templates will have 10 such page a tag, when one day, you suddenly want to change the address of the term,

Urlpatterns = Patterns ("',

(R ' ^news$ ', ' News_index '),

)

You will find that you have to change 10 in templates

<a href= "/article" > News </a>

Yes

<a href= "/news" > News </a>

Hateful is that the labels are distributed on different pages,

There are worse times when you don't know exactly how many such a tags are (never a single number).

With the URL, the situation is greatly different.

Urlpatterns defines the home address of the information,

Urlpatterns = Patterns ("',

URL (R ' ^article$ ', ' News_index ',name= "News_index"),

)

The HTML in the templates is

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

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

This is used in templates

{%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:

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

There are two parameters, the final address such as the archived address http://www.baoyalv.com/2010/02

The situation becomes more complicated, and the usage of urlpatterns is the same:

URL (r ' ^ (? P<YEAR>\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:

<a href= "{%url news_archive, 02%}" >2010 year February </a>

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:

Reverse ("news_archive", kwargs={"year":, "month": 02})

Then, the last resolved address is "/2010/02".

The meaning of the name parameter of the Django URL

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.