利用url中get传参,打开一个新页面
views.py中定义函数,并在urls填写好路由信息:
def index(request):
return render(request,'index.html',{'user':USER_DICT})
def detail(request):
#点击跳转详细界面,在url中get方式传参数
nid=request.GET.get('nid')
detail_info=USER_DICT[nid]
return render(request,'detail.html',{'detail_info':detail_info})
urlpatterns = [
path('index/', views.index),
path('detail/',views.detail)
]
index.html中:
{# 点击跳转详细界面,url中传参#}
{% for foo,v in user.items %}
- {{ v.name }}
{% endfor %}
detail.html中:
详细信息
用户名:{{ detail_info.name }}
是否为超级管理员:{{ detail_info.super }}
1.基础方法:
一个类url对应一个函数
在views.py和urls.py中修改好:
def index(request):
return render(request,'index.html',{'user':USER_DICT})
def detail(request,nid):
#一类url对应一个函数
detail_info=USER_DICT[nid]
return render(request,'detail.html',{'detail_info':detail_info})
此处是在新建的APP文件中新建一个urls.py中修改
from django.conf.urls import url
from . import views
urlpatterns = [
url('login/', views.login),
url('home/',views.Home.as_view()),
url('index/', views.index),
# url('detail/',views.detail),
url(r'detail-(\d+)', views.detail, name="index"), #一类url对应一个函数
]
index.html中修改:
{# 一个类的url对应一个函数#}
{% for foo,v in user.items %}
- {{ v.name }}
{% endfor %}
detail.html不做修改
2.规范写法(推荐写法)
views.py和urls.py中,分别修改detail的形参和路径中的正则表达式:
def detail(request,*args,**kwargs):
#一类url对应一个函数,传列表和字典
nid=kwargs['nid']
detail_info=USER_DICT[nid]
return render(request,'detail.html',{'detail_info':detail_info})
urlpatterns = [
url('login/', views.login),
url('home/',views.Home.as_view()),
url('index/', views.index),
# url('detail/',views.detail),
# url(r'detail-(\d+)-(\d+).html', views.detail, name="index"), #一类url对应一个函数
url(r'detail-(?P\d+)-(?P\d+).html', views.detail), #一类url对应一个函数
]
index.html可以不做修改,这里为了便于观察结果,稍作调整:
{# 点击跳转详细界面,url中传参#}
{% for foo,v in user.items %}
- {{ v.name }}
{% endfor %}
路由对应的是url中的路径信息,Django提供了便于修改其路径信息的方法
1.初级使用
在urls.py中定义,增添name属性:
urlpatterns = [
url('login/', views.login),
url('home/',views.Home.as_view()),
url('myindex/', views.index,name='index'),
]
模板语言中调用,修改action的提交路径,注意空格:
2.在当前url中提交到指定url路径下
views.py和urls.py中依次修改相应的函数:
def index(request,nid):
#需要接收获取到的数字
return render(request,'index.html',{'user':USER_DICT})
urlpatterns = [
url('myindex/(\d+)', views.index,name='index')
]
模板语言中修改action(这里只是增加数字1):
3.在当前url中提交到当前url路径
views.py和urls.py中的信息同上,这里只需要修改模板语言中的action:
4.提交当前url到指定url路径的其他写法:
4.1 在views.py中直接修改request.path_info的值
def index(request,nid):
print(request.path_info)
# 1.直接写死,在这里修改path_info的值
request.path_info='/myindex/40'
return render(request, 'index.html', {'user': USER_DICT})
4.2 在views.py中调用函数,执行django.urls的reverse函数
a.对于列表型的参数:
def index(request,nid):
print(request.path_info)
# 1.直接写死,在这里修改path_info的值
# request.path_info='/myindex/40'
#2.方法2
from django.urls import reverse
v=reverse('index',args=(40,))
request.path_info=v
return render(request,'index.html',{'user':USER_DICT})
模板语言index.html中不做修改,这里仅做展示
b.对于字典型的参数:
首先应在urls.py中定义:
urlpatterns = [
url('login/', views.login),
url('home/',views.Home.as_view()),
url('myindex/(?P\d+)/(?P\d+)', views.index,name='index'),
]
然后在views.py中修改:
def index(request,*args,**kwargs):
print(request.path_info)
# 1.直接写死,在这里修改path_info的值
# request.path_info='/myindex/40'
#2.方法2
from django.urls import reverse
v=reverse('index',kwargs={"nid":1,"pid":99})
request.path_info=v
return render(request,'index.html',{'user':USER_DICT})
index.html中不做修改,这里仅做展示:
5.在模板语言中修改url提交的路径
action="{% url 'index' %}"
action="{% url 'index' 1 92 %}"
action="{% url 'index' nid=1 pid=92 %}"
1.多级路由
在project下的urls.py中修改:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('app01.urls')),
]
在app目录下创建一个urls.py中修改:
from django.conf.urls import url
from . import views
urlpatterns = [
url('login/', views.login),
url('home/',views.Home.as_view()),
url('myindex/(?P\d+)/(?P\d+)', views.index,name='index'),
]
2.命名空间
在project的urls.py中修改:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path(r'shop/', include('shop.urls', namespace='shop-polls')),
path(r'consumer/', include('consumer.urls', namespace='consumer-polls')),
]
分别修改相应的app文件的urls.py:
from django.conf.urls import url
from . import views
app_name = 'shop'
urlpatterns = [
url(r'^(?P\d+)/$', views.detail, name='detail')
]
修改对应app文件下的views.py文件
def detail(request, nid):
print(request.resolver_match)
return HttpResponse(nid)
定义带命名空间的url之后,使用name生成URL时候,应该如下,按照相应的方式来填写:
v = reverse('shop:detail', kwargs={'nid':11})