我继承了Django应用,并注意到urlpatterns += patterns('')
和整个urls.py
中的等效项。
例如
urlpatterns = patterns(
'',
url(r'^index.html', render_index),
)
#...
urlpatterns += patterns(
'',
url(r'^page.html', another_controller),
)
这是在做什么有什么事吗
最佳答案
patterns()
函数中需要它,因为patterns()的第一个参数用作URL的公共视图前缀。从文档:
urlpatterns = patterns('',
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
可以更简单地写为:
urlpatterns = patterns('news.views',
(r'^articles/(\d{4})/$', 'year_archive'),
(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'),
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'),
)
但是,从Django 1.8开始,
urlpatterns
中的urls.py
变量是用一个简单的列表创建的:urlpatterns = [
url(r'^index.html', render_index),
url(r'^page.html', another_controller),
]
并且不需要此视图前缀参数。
关于python - Django中的空白网址格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40835760/