Django中的空白URL模式

时间:2016-11-28 01:03:15

标签: python django django-urls

我继承了一个Django应用,并注意到urlpatterns += patterns('')urls.py中的等效内容。

e.g。

urlpatterns = patterns(
    '', 
    url(r'^index.html', render_index),
)

#... 

urlpatterns += patterns(
    '', 
    url(r'^page.html', another_controller),
)

这是做什么的?什么?

1 个答案:

答案 0 :(得分:2)

typedef struct PROFI_tstPartition { uint32 xAddressID; uint32 u32Reserved1; uint32 xBlockTableAdr; uint32 u32Reserved2; uint16 u16NumberOfBlocks; uint16 u16GlobalProperties; uint8 au8Reserved[4]; } PROFI_tstPartition; typedef struct PROFI_tstBlock { uint32 xPhysicalAddress; uint32 u32Reserved1; uint32 xLogicalAddress; uint32 u32Reserved2; uint32 xBlockLength; uint32 u32Reserved3; uint32 xSectorSize; uint32 u32Reserved4; uint8 u8BlockMemoryType; uint8 u8Security; uint16 u16Reserved; uint16 u16BlockProperties; uint16 u16Reserved2; } PROFI_tstBlock; 函数需要它,因为patterns()的第一个参数被用作网址的公共视图前缀。来自文档:

patterns()

更简单地写成:

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'),
)

但是,自Django 1.8以来,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'), ) 中的urlpatterns变量是使用简单列表创建的:

urls.py

并且不需要此视图前缀参数。