Skip to main content
 首页 » 编程设计

python之如何创建一个 'urlpattern' 数量不确定的 'named regex groups'

2024年10月17日8sxdcgaq8080

解释

我希望我的网站 URL 具有文件夹类型结构,如下例所示:

www.sitename.com/catagory/sub-catagory/another-catagory/yet-another 

目前在我的 urls.py 中,我有以下 urlpattern 来开始描述它:

url(r'^(?P<slug>[a-zA-Z0-9-\/]+)/', views.page_view) 

我的问题

url好像只能获取到url的最后一部分,所以如果我输入下面的

www.sitename.com/catagory/sub-catagory/another-catagory/yet-another 

它似乎只是捕捉

yet-another 

我不知道如何解决这个问题,任何帮助将不胜感激:)

请您参考如下方法:

这似乎工作正常

# urls.py  
url(r'^(?P<slug>[a-zA-Z0-9-\/]+)/', views.page_view) 
 
# views.py 
def page_view(request, slug): 
    slug  #  

对于 url www.example.com/a/b/c/d 它将捕获 a/b/c 可以在 处拆分/。

对于 url www.example.com/a/b/c/d/ 它将捕获 a/b/c/d