反向解析
-
模板中的反向解析
-
在test4/urls.py中为include定义namespace属性。
url(r'^',include('booktest.urls',namespace='booktest')),
-
在booktest/urls.py中为url定义name属性,并修改为fan2。
url(r'^fan2/$', views.fan2,name='fan2'),
-
在模板中使用url标签做超链接,此处为templates/booktest/fan1.html文件。
<html> <head> <title>反向解析</title> </head> <body> 普通链接:<a href="/fan2/">fan2</a> <hr> 反向解析:<a href="{%url 'booktest:fan2'%}">fan2</a> </body> </html>
-
视图中的反向解析。
-
from django.shortcuts import redirect from django.core.urlresolvers import reverse return redirect(reverse('booktest:fan2'))