我正在处理一些较旧的Django代码,并且url
函数未在任何地方使用,类似于the Django 1.4 documentation中的示例:
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
(r'^articles/2003/$', 'news.views.special_case_2003'),
(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'),
)
但我注意到the Django 1.5 documentation经常使用url
函数:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^articles/2003/$', 'news.views.special_case_2003'),
url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
这是为什么?这是一个约定问题,还是有技术原因要使用url
函数?我应该在将来遵循哪种做法,以及如何在没有url
来电的情况下维护我的遗留代码?
答案 0 :(得分:2)
来自docs
url(regex,view,kwargs = None,name = None,prefix ='')
您可以使用url()函数而不是元组作为patterns()的参数。如果要指定不带可选额外参数字典的名称,这很方便。例如:
urlpatterns = patterns('',
url(r'^index/$', index_view, name="main-view"),
...
)
您可以将它们用于反向网址匹配(同样,docs)
您可以将第一个示例转换为:
url(r'^articles/2003/$', special_case_2003, name="special_case_2003"),
并在模板中调用
{% url special_case_2003 %}
是的,也许你发布的两个例子对于这个
来说太模糊了