Django URL Match

Source: Internet
Author: User

Study site: official

The following is a personal excerpt:

1. Basic Matching

Urlpatterns = Patterns (",    url (r ' ^articles/2003/$ ', ' news.views.special_case_2003 '),    url (r ' ^articles/(\d{ 4})/$ ', ' news.views.year_archive '),    url (r ' ^articles/(\d{4})/(\d{2})/$ ', ' news.views.month_archive '),    URL (R ' ^articles/(\d{4})/(\d{2})/(\d+)/$ ', ' News.views.article_detail '),)

/articles/2005/03/ This matches the third URL, which is News.views.mouth_archive (2005, 03)

/articles/2003/03/03/ match 4th, i.e. news.views.article_detail (Request, ' 2003 ', ' the ', ') ' .

2. Keyword Matching

Urlpatterns = Patterns (',    url (r ' ^articles/2003/$ ', ' news.views.special_case_2003 '),    url (r ' ^articles/(? P<YEAR>\D{4})/$ ', ' news.views.year_archive '),    url (r ' ^articles/(? P<YEAR>\D{4})/(? P<MONTH>\D{2})/$ ', ' news.views.month_archive '),    url (r ' ^articles/(? P<YEAR>\D{4})/(? P<MONTH>\D{2})/(? P<DAY>\D{2})/$ ', ' News.views.article_detail '),)

/articles/2005/03/ is news.views.month_archive (request, year= ' 2005 ', month= ') , instead of the news.views.month_archive (Request, ' 2005 ', ' A ' ).

Is it possible to get all the URL parameters? Of course

3. Default value Matching

Urlpatterns = Patterns (',    url (r ' ^blog/$ ', ' blog.views.page '),    url (r ' ^blog/page (? p<num>\d+)/$ ', ' Blog.views.page '), # View (in blog/views.py) def page (Request, num= "1"):    # Output the Appropriate page of blog entries, according to Num.    ...

If the first match is reached, num defaults to 1.

4. Model

If:

Urlpatterns = Patterns (",    url (r ' ^articles/(\d{4})/$ ', ' news.views.year_archive '),    url (r ' ^articles/(\d{4} )/(\d{2})/$ ', ' news.views.month_archive '),    url (r ' ^articles/(\d{4})/(\d{2})/(\d+)/$ ', ' News.views.article_ Detail '),)

It can be replaced by:

Urlpatterns = Patterns (' news.views ',    url (r ' ^articles/(\d{4})/$ ', ' year_archive '),    url (r ' ^articles/(\d{4}) /(\d{2})/$ ', ' month_archive '),    url (r ' ^articles/(\d{4})/(\d{2})/(\d+)/$ ', ' Article_detail '),)

Note that the first parameter of the patterns changes

5. Multi-model

If:

Urlpatterns = Patterns (',    url (r ' ^$ ', ' Myapp.views.app_index '),    url (r ' ^ (? P<YEAR>\D{4})/(? P<MONTH>[A-Z]{3})/$ ', ' Myapp.views.month_display '),    url (r ' ^tag/(? p<tag>\w+)/$ ', ' Weblog.views.tag '),)

It can be replaced by:

Urlpatterns = Patterns (' myapp.views ',    url (r ' ^$ ', ' App_index '),    url (r ' ^ (? P<YEAR>\D{4})/(? P<MONTH>[A-Z]{3})/$ ', ' Month_display '), urlpatterns + = patterns (' weblog.views ',    url (r ' ^tag/(? p<tag>\w+)/$ ', ' tag '),

6.include

Urlpatterns = Patterns ("',    # ... snip    ... URL (r ' ^comments/', include (' Django.contrib.comments.urls ')),    URL (r ' ^community/', include (' Django_ Website.aggregator.urls '),    url (r ' ^contact/', include (' Django_website.contact.urls ')),    # ... snip ...)

The following usage can also be:

Extra_patterns = Patterns ("",    url (r ' ^reports/(? p<id>\d+)/$ ', ' Credit.views.report '),    url (r ' ^charge/$ ', ' Credit.views.charge '), Urlpatterns = Patterns (' ', url (r '    ^$ ', ' apps.main.views.homepage '),    url (r ' ^help/', include (' Apps.help.urls ')),    URL (r ' ^credit/ ', include (Extra_patterns)),)

Is this complicated?

Urlpatterns = Patterns (' wiki.views ',    url (r ' ^ (? p<page_slug>\w+)-(? p<page_id>\w+)/history/$ ', ' history '),    url (r ' ^ (? p<page_slug>\w+)-(? p<page_id>\w+)/edit/$ ', ' edit '),    url (r ' ^ (? p<page_slug>\w+)-(? p<page_id>\w+)/discuss/$ ', ' discuss '),    url (r ' ^ (? p<page_slug>\w+)-(? p<page_id>\w+)/permissions/$ ', ' permissions '),)

Replace it with the following:

Urlpatterns = Patterns ("",    url (r ' ^ (? p<page_slug>\w+)-(? p<page_id>\w+)/', Include (Patterns (' wiki.views ',        url (r ' ^history/$ ', ' history '),        url (r ' ^edit/$ ', ' Edit '),        url (r ' ^discuss/$ ', ' discuss '),        url (r ' ^permissions/$ ', ' permissions '),)))    

7. Additional Parameters

Urlpatterns = Patterns (' blog.views ',    url (r ' ^blog/(? P<YEAR>\D{4})/$ ', ' year_archive ', {' foo ': ' Bar '}),

/blog/2005/, i.e. blog.views.year_archive (request, year= ' 2005 ', foo= ' bar).

Django URL Match

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.