Django模式方法的第一个参数有什么用?

时间:2011-11-02 11:01:50

标签: python django

第一个参数(在下面的示例中包含'')对patterns有什么意义?

urlpatterns = patterns('',
    (r'^articles/2003/$', 'news.views.special_case_2003'),
    (r'^articles/(\d{4})/$', 'news.views.year_archive'),
)

有时我看到它被填写,如下:

urlpatterns = patterns(
    'zinnia.views.entries',
    url(r'^$',
        'entry_index', entry_conf_index,
        name='zinnia_entry_archive_index'),
    )

2 个答案:

答案 0 :(得分:6)

它是视图路径的前缀。请参阅documentation for patterns

使用此参数的第一个示例相当于:

urlpatterns = patterns('news.views',
    (r'^articles/2003/$', 'special_case_2003'),
    (r'^articles/(\d{4})/$', 'year_archive'),
)

将第一个参数设置为''的第二个示例相当于:

urlpatterns = patterns('',
    url(r'^$',
        'zinnia.views.entries.entry_index', entry_conf_index,
        name='zinnia_entry_archive_index'),
    )

答案 1 :(得分:1)

它是所有网址的前缀,例如您可以在您的情况下添加news.views

urlpatterns = patterns('news.views',
    (r'^articles/2003/$', 'special_case_2003'),
    (r'^articles/(\d{4})/$', 'nyear_archive'),
)