简体   繁体   English

django URL在URL路径中以正则表达式结尾

[英]django Url endswith regex in url path

I need to support following urls in single url regex. 我需要在单个网址正则表达式中支持以下网址。

/hotel_lists/view/
/photo_lists/view/
/review_lists/view/

how to support all above urls in single views? 如何在单个视图中支持以上所有网址?

I tried something like below 我尝试了如下

url(r'^\_lists$/(?P<resource>.*)/$', 'admin.views.customlist_handler'),

edit: hotel,photo, review is just example. 编辑:酒店,照片,评论只是示例。 that first part will be dynamic. 第一部分将是动态的。 first part can be anything. 第一部分可以是任何东西。

If you wish to capture the resource type in the view, you could do this: 如果您希望在视图中捕获资源类型,则可以执行以下操作:

url(r'^(?P<resource>hotel|photo|review)_lists/view/$', 'admin.views.customlist_handler'),

Or to make it more generic, 或使其更通用

url(r'^(?P<resource>[a-z]+)_lists/view/$', 'admin.views.customlist_handler'), #Or whatever regex pattern is more appropriate

and in the view 并认为

def customlist_handler(request, resource):
    #You have access to the resource type specified in the URL.
    ...

You can read more on named URL pattern groups here 您可以在此处阅读有关命名URL模式组的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM