0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

first part of url , hotel, review, photo is just example. some other resource will also be start with. can you suggest how to do?
Check the edit.. The second URL pattern in the answer addresses exactly that. Note that I am assuming that the resource has only lowercase alphabets. You can modify the regex pattern to accommodate your usecase. .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.