2017-10-05 69 views
0

我知道在每個應用程序中,我們可以使用自己的urlpatterns並使用include將其包含在主項目/應用程序中。可能將同一應用程序的urlpatterns分組?django

我想知道如果一個應用程序有幾個不同的網址,有沒有辦法將它分組?

例如

urlpatterns = [ 
    url(r'^user/$', hello.asView(), 
    url(r'^user/hello/$', hello.asView(), 
    url(r'^user/there/$', hello.asView(), 
    url(r'^user/here/$', hello.asView(), 
    url(r'^user/that/$', hello.asView(), 
    url(r'^user/mini/$', hello.asView(), 
    url(r'^user/max/$', hello.asView(), 

    url(r'^bb/$', hello.asView(), 
    url(r'^bb/hello/$', hello.asView(), 
    url(r'^bb/there/$', hello.asView(), 
    url(r'^bb/here/$', hello.asView(), 
    url(r'^bb/that/$', hello.asView(), 
    url(r'^bb/mini/$', hello.asView(), 
    url(r'^bb/max/$', hello.asView(), 
] 

請忽略所有的hello.asView(),但我想知道如果有一種方法將所有的userbb所以如果有多個URL,我不需要繼續鍵入user還是bb

在此先感謝您的幫助。

回答

0

您可以使用匹配模式

url(r'^user/(?P<page>[\w-]+)/$', hello.asView(), 

,並在您的視圖,你可以檢查頁面是否有效。假設你有一個頁面列表。你只是這樣做:

def get(self, page): 
    page_list = ['hello', 'there', 'here'] 
    if page not in page_list: 
     return HttpResponse(status=404) 
    return HttpResponse(status=200) 
相關問題