Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...