反向解析
-
模板中的反向解析
-
在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'))
博客主要介绍Django反向解析,包括模板中的反向解析,需在test4/urls.py中定义namespace属性,在booktest/urls.py中定义name属性并修改为fan2,在模板文件中用url标签做超链接;还提及视图中的反向解析。
467

被折叠的 条评论
为什么被折叠?



