测试了四种情形,具体请看index.html中的注释部分
views.py 代码
1 from django.shortcuts import render,HttpResponse,redirect,reverse 2 3 def index(request): 4 return render(request,'index.html') 5 6 def book(request): 7 return HttpResponse('图书页面') 8 9 def movie(request, movie_id): 10 return HttpResponse('电影页面') 11 12 def city(request): 13 next_1 = request.GET.get('next') 14 text = '同城页面,跳转的url是:%s' % next_1 15 return HttpResponse(text)
urls.py 代码
1 from django.urls import path 2 from booktest import views 3 4 urlpatterns = [ 5 # path('^article/$', views.article), 6 path('', views.index), 7 path('book/', views.book, name='book'), 8 path('movie/<movie_id>/', views.movie, name='movie'), 9 path('city/', views.city, name='city'), 10 ]
index.html 代码
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板渲染</title> 6 </head> 7 <body> 8 <ul> 9 <li><a href="/">首页</a></li> 10 <!-- 直接使用链接 --> 11 12 <li><a href="{% url 'book' %}">书城</a></li> 13 <!-- 使用别名 --> 14 15 <li><a href="{% url 'movie' movie_id='1' %}">电影</a></li> 16 <!-- 传递参数 --> 17 18 <li><a href="{% url 'city' %}?next=/">同城</a></li> 19 <!-- 查询字符串的参数 --> 20 </ul> 21 </body> 22 </html>