Django Notes II: URL Dispatcher

Source: Internet
Author: User

Typically there will be a root_urlconf in settings.py, and Django will look for a route from the root_urlconf point to the file that the Urlpatterns variable is configured for.

Urlpatterns can also include urlpatterns in other model, Django will find from top to bottom to see if the URL matches. After matching, the corresponding view processing is given.

Urlpatterns = [    url (r'^admin/', include (Admin.site.urls)),    URL (r  '^index/$', view_index),    URL (r'^blog/ ' , include (Blog_urls)),]

The non-named capturing group in the URL is received in the view with the position parameter, actually receives the tuple, you receive the same with *args, the tuple will automatically unpack to the corresponding location

Urlpatterns = [    url (r'^articles/([0-9]{4})/$', views.year_archive),    URL (r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),]
def month_archive (request, year, month):     Pass

The named capturing group is received with the keyword parameter, actually receives a dictionary, the dictionary will automatically unpack to the corresponding field, you receive with **kw and then own solution is same

Urlpatterns = [    url (r'^articles/(? P<YEAR>[0-9]{4})/$', views.year_archive),    URL (r'^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/$', views.month_archive),]
def month_archive (request, year, month):    Pass

All parameters are received as strings, regardless of the type in your regular (captured arguments is always strings)

This place in development encountered several pits, the background RPC interface received is shaping, and I passed the string, resulting in an incorrect sorting, also thought that there is a problem with the RPC interface, the level of the dish.

Override the default 404 500 Interface, just assign a custom view to django.conf.urls.handler404 in root_urlconf

django.conf.urls.handler404 == view_500

Reverse URL, to Reverse a URL first name

Non-named parameter reverse

Urlpatterns = [    url (r'^articles/([0-9]{4})/$', view_articles, Name='  article')]print(reverse ('article') , args= (2015,)))

Named parameter reverse

defview_articles (Request, year):returnHttpResponse (content='Hello World') Urlpatterns=[url (r'^articles/(? P<YEAR>[0-9]{4})/$', View_articles, Name='article')]Print(Reverse ('article', kwargs={' Year': 2018}))

Django Notes II: URL Dispatcher

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.