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 ```html
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li> <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 %} {% if latest_question_list %}
<ul> <ul>
{% for question in latest_question_list %} {% 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 %} {% endfor %}
</ul> </ul>
{% else %} {% else %}

View File

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