为什么需要命名空间来区分不同应用模板中的标识符?

Why is namespace required to differentiate identifiers in templates for different apps?

Namespacing URL names 上的 Django 教程描述了 main urlpatterns 可以在为应用包含 urls 时指定 namespace,例如:

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    ...

然后命名空间用于在渲染模板时区分引用,如'polls:detail',如:

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

但是,据我了解,应用程序已经根据目录位置进行了区分,例如在本例中 "pools/" 对应 "pools" 应用程序,那么为什么会出现此 namespace 指示需要,当模板位于 app 目录下时?

使用 namespace 是否意味着必须修改导入应用程序的模板才能匹配 include 中选择的 namespace 值?

命名空间可能很有用,因为不同的应用程序有 url 同名。您如何区分 polls 应用程序中的 detail url 与 profiles 应用程序中的 detail url?使用名称空间,您可以使用 polls:detailprofiles:detail 而不会发生任何冲突。

命名空间还允许您将一个应用附加到单个项目中的不同 urls。

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^other_polls/', include('polls.urls', namespace="other_polls")),
    ...
)