标签:个数 ref 根据 需要 conf 路由 fas tail cmdb
1、url(r‘^index/‘, views.index), 函数处理
url(r‘^home/‘, views.Home.as_view()), 类方法处理
2、url(r‘^detail-(\d+).html‘, views.detail), 动态路由,位置参数
<a href="/detail-{{key}}"></a>
def detail(request,nid):
3、url(r‘^detail-(?P<nid>\d+)-(?P<uid>\d+).html‘, views.detail) 动态路由,命名参数(将URL第一个匹配的给nid),而后台函数处理时,第一个数一定是nid
PS: def detail(request, *args,**kwargs):
pass
实战:
a.
url(r‘^detail-(\d+)-(\d+).html‘, views.detail),
def func(request, nid, uid):
#不用id直接获取值是应为python有内置的函数id()
#,如果使用ID接收参数,那么原生的id函数将被重写
pass
def func(request, *args):
args = (2,9)
def func(request, *args, **kwargs):
args = (2,9)
b.
url(r‘^detail-(?P<nid>\d+)-(?P<uid>\d+).html‘, views.detail)
def func(request, nid, uid):
pass
def funct(request, **kwargs):
kwargs = {‘nid‘: 1, ‘uid‘: 3}
def func(request, *args, **kwargs):
args = (2,9)
4、 name
对URL路由关系进行命名, ***** 以后可以根据此名称生成自己想要的URL *****
①url(r‘^asdfasdfasdf/‘, views.index, name=‘i1‘),->
<form action="{%url :‘i1‘%}"> /index/ 如果想要和当前URL一样 需要使用path_info变量 {{request.path_info}}
<form action="{%url :‘i1‘2 %}"> /index/2/
<form action="{%url :‘i1‘nid=2 uid=3 %}"> /index/2/3/
②url(r‘^yug/(\d+)/(\d+)/‘, views.index, name=‘i2‘),
③url(r‘^buy/(?P<pid>\d+)/(?P<nid>\d+)/‘, views.index, name=‘i3‘),
2,3是动态的URL,不能用1的方法指定。只能用
<form action="{{ request.path_info }}">
反向生成URL
④url(r‘^yug/(\d+)/(\d+)/‘, views.index, name=‘i2‘)
from django.urls import reverse 根据名字反转生成URL,函数处理使用
v = reverse("i2",args)=(90,80)
v就等于 [ *,*,*,*:*/yug/90/80/ ]
def func(request, *args, **kwargs):
from django.urls import reverse
url1 = reverse(‘i1‘) # asdfasdfasdf/
url2 = reverse(‘i2‘, args=(1,2,)) # yug/1/2/
url3 = reverse(‘i3‘, kwargs={‘pid‘: 1, "nid": 9}) # buy/1/9/
xxx.html
{% url "i1" %} # asdfasdfasdf/
{% url "i2" 1 2 %} # yug/1/2/
{% url "i3" pid=1 nid=9 %} # buy/1/9/
注:
# 当前的URL
request.path_info
☆5、多级路由 一个APP需要路由到另一个APP时
project/urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r‘^cmdb/‘, include("app01.urls")),
# /cmdb/**的URL转发给 app01.urls
url(r‘^monitor/‘, include("app02.urls")),
]
app01/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r‘^login/‘, views.login),
]
app02/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app02 import views
urlpatterns = [
url(r‘^login/‘, views.login),
]
Django学习-6-路由系统
标签:个数 ref 根据 需要 conf 路由 fas tail cmdb
原文地址:http://www.cnblogs.com/cq146637/p/7806274.html