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