Django DTL模板语法中的url反转

 1 """template_url_demo URL Configuration
 2 
 3 The `urlpatterns` list routes URLs to views. For more information please see:
 4     https://docs.djangoproject.com/en/2.1/topics/http/urls/
 5 Examples:
 6 Function views
 7     1. Add an import:  from my_app import views
 8     2. Add a URL to urlpatterns:  path('', views.home, name='home')
 9 Class-based views
10     1. Add an import:  from other_app.views import Home
11     2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
12 Including another URLconf
13     1. Import the include() function: from django.urls import include, path
14     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
15 """
16 from django.urls import path
17 from . import views
18 
19 urlpatterns = [
20     path('', views.index,name='index'),
21     path('book/',views.book,name='book'),
22     path('movie/',views.movie,name='movie'),
23     path('city/',views.city,name='city'),
24     path('book/detail/<book_id>/<category>',views.book_detail,name='book_detail'),
25     path('login/',views.login,name='login')
26 ]
urls.py
 1 from django.shortcuts import render
 2 from django.http import HttpResponse
 3 
 4 def index(request):
 5     context={
 6         'a':'a'
 7     }
 8     return render(request,'index.html',context=context)
 9 
10 def book(request):
11     return HttpResponse('读书页面')
12 
13 def movie(request):
14     return HttpResponse('电影页面')
15 
16 def city(request):
17     return HttpResponse('同城页面')
18 
19 
20 def book_detail(request,book_id,category):
21     text='您的图书的ID是:%s,分类是:%s' %(book_id,category)
22     return HttpResponse(text)
23 
24 def login(request):
25     next=request.GET.get('next')
26     text='登录页面,登录完成后要跳转的url是:%s' % next
27     return HttpResponse(text)
views.py
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .nav{
            overflow: hidden;
        }
        .nav li {
            float: left;
            list-style: none;
            margin: 0 20px;
        }

    </style>
</head>
<body>
    <ul class="nav">
        <li><a href="/">首页</a></li>
        <li><a href="{% url 'book' %}">读书</a></li>
        <li><a href="{% url 'movie' %}">电影</a></li>
        <li><a href="{% url 'city' %}">同城</a></li>
        <li><a href="{% url 'book_detail' book_id='1' category='1' %}">最火的一篇文章</a></li>
        <li><a href="{% url  'login'  %}?next=/">登录</a></li>
    </ul>
</body>
</html>
index.html

 

posted @ 2019-01-22 15:55  random_lee  阅读(476)  评论(0编辑  收藏  举报