擅长:python、mysql、java
<p>它的工作相当直观。<code>include</code>具有另一个命名空间的urlconf将导致嵌套命名空间。</p>
<pre><code>## urls.py
nested2 = patterns('',
url(r'^index/$', 'index', name='index'),
)
nested1 = patterns('',
url(r'^nested2/', include(nested2, namespace="nested2"),
url(r'^index/$', 'index', name='index'),
)
urlpatterns = patterns('',
(r'^nested1/', include(nested1, namespace="nested1"),
)
reverse('nested1:nested2:index') # should output /nested1/nested2/index/
reverse('nested1:index') # should output /nested1/index/
</code></pre>
<p>这是一个很好的方式来保持网址的组织。我想我能给出的最好建议是记住<code>include</code>可以直接接受一个<code>patterns</code>对象(如我的示例),它允许您使用单个<code>urls.py</code>并将视图拆分为有用的名称空间,而不必创建多个url文件。</p>