Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...