3.4 Remove hardlink URL in template

master
Jason Zhu 2020-10-20 16:37:38 +11:00
parent f91a6b6e25
commit aae69f930b
2 changed files with 18 additions and 2 deletions

View File

@ -223,4 +223,20 @@ Here `path()` function is passed **route** and **view**; two additional option a
</ul>
```
模板系统统一使用点符号来访问变量的属性
模板系统统一使用点符号来访问变量的属性
### 3.5 Remove hardlink URL in template
In template `polls/index.html`, links are hardcoded as shown
```html
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
```
Consider `index.html` will be reused numerous times, 去耦合is necessary.
```html
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
```

View File

@ -1,7 +1,7 @@
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}