django Url在网址路径中以正则表达式结尾 [英] django Url endswith regex in url path

查看:305
本文介绍了django Url在网址路径中以正则表达式结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在单个网址正则表达式中支持以下网址。

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?

我尝试了以下操作

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

或者使其更通用,

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

在视图中

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

您可以在在此命名URL模式组

这篇关于django Url在网址路径中以正则表达式结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆