Django-url Routing System

Source: Internet
Author: User

Django's routing system: First, basic usage:
1.path (' index ', Views.index), # Create a URL map by way of the class 2.path (' home ', views. Home.as_view ()), # By means of regular expressions note that this method cannot be associated by path, otherwise the regular expression is invalid, through the From Django.conf.urls import URL, and then through the URL (regx,views, Kwargs,name) matches the regular expression of the URL 3.url (R ' detail-(\d+) ', Views.detail),4.url (R ' detail-(\d+)-(\d+) ', Views.detail),5.url (R ' detail-(? p<nid>\d+) ', Views.detail)
Combat: 1. Via URL (R ' detail-(\d+)-(\d+) ', Views.detail)

  urls.py

= [    url (r' detail-(\d+)-(\d+) ', Views.detail),   ]

views.py

# Way One: def detail (request,nid,uid):    pass# Way Two: def detail (request,*args,**Kwargs):    Pass

In the above way, the parameters associated with NID and UID are appropriate for calling parameters, such as calling ' Http://127.0.0.1:8080/detail-1-2 ', then nid = 1, uid = 2;

But that's not what we want, how do we get nid to associate with the UID and the passed parameters, or to resolve the parameter association problem with regular expressions

2. Via the URL (r ' detail-(? p<nid>\d+)-(? p<uid>\d+) ', Views.detail)

urls.py

Urlpatterns = [    url (r' detail-(? p<nid>\d+)-(? p<uid>\d+) ', Views.detail)]

views.py

# Way One: def detail (request,nid,uid):    Pass

def detail (Request,uid,nid):
pass# Way II: DEF detail (request,*args,**Kwargs): Pass

So no matter how we change the position of the parameter, our obtained NID is the first parameter value of '/detail-1-2 ', the UID is always the second parameter value, and the parameter of mode two becomes args= () kwargs={' nid ': 1, ' UID ': 2}

Second, advanced 1.name (name the URL routing relationship, you can later generate the URL you want by name)

urls.py

Urlpatterns = [    url (' homemodule ', Views.home,name= ' M1 '),    url (r' disovermodule/(\d+)/(\d+ ) ', views.discover,name= ' m2 '),    url (r' messagemodule/(? p<nid>\d+)/(? p<uid>\d+) ', views.message,name= ' m3 ')    ]    

views.py

def func (request,*args,**Kwargs): From    django.url import reverse        = reverse (' m1 ')                     #/homemodule/      = reverse (' m2 ', args= ())    #/discovermodule/1/2    url3 = reverse (' m3 ', kwargs={' nid ': 3, ' UID ': 4 })    #/MESSAGEMODULE/3/4

Xxx.html

{% URL ' m1 '%}          #/homemodule

{% url ' m3 ' nid=3 uid=4%} #/messagemodule/3/4

Note get the current URL

Request.path_info

Django-url Routing System

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.