目录
去除模板中的硬编码 URL
<li><a href="/polls/{
{ question.id }}/">{
{ question.question_text }}</a></li>
问题在于,硬编码和强耦合的链接,对于一个包含很多应用的项目来说,修改起来是十分困难的。然而,因为你在 polls.urls
的 url()
函数中通过 name 参数为 URL 定义了名字,你可以使用 {% url %}
标签代替它:
<li><a href="{% url 'detail' question.id %}">{
{ question.question_text }}</a></li>
具有名字 'detail' 的 URL 是在如下语句中定义的:
...
# the 'name' value as called by the {% url %} template tag
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
...
如果你想改变投票详情视图的 URL,比如想