3.6 Add namespace for URL

master
Jason Zhu 2020-10-20 16:45:04 +11:00
parent aae69f930b
commit 624f61a563
3 changed files with 10 additions and 2 deletions

View File

@ -239,4 +239,11 @@ Consider `index.html` will be reused numerous times, 去耦合is necessary.
```html
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
```
```
### 3.6 为 URL 名称添加命名空间
在根 URLconf 中添加命名空间 (namespace)
1. Add `app_name = 'polls'` for namespace in `poll.urls.py`
2. Change `detail` in `index.html` to `polls:detail`

View File

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

View File

@ -2,6 +2,7 @@ from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),