1

I have multiple urls whose mission is almost identical (generate a list page with the corresponding context). So my urlpatterns is something like:

url('^by-country/(?<arg1>\w+)', MyO.as_view(), name='by-country'),
url('^by-period/(?<arg1>\w+)', MyO.as_view(), name='by-period'),
url('^by-age/(?<arg1>\w+)', MyO.as_view(), name='by-age')

Is there a way to pass the by-blabla string in a one class based view without making it as a second regex named argument in my urlpatterns?

Since I use a View derived class (i.e. MyO), I cannot pass anything to it except already-declared attributes (e.g template_name). So, is there any clean way to do it?

1 Answer 1

1

All url patterns accept a third positional argument, which is a dict of extra parameters to pass to the view that are not derived from the regex itself. So:

url('^by-country/(?<arg1>\w+)', MyO.as_view(), {'category': 'by-country'}, name='by-country'),
Sign up to request clarification or add additional context in comments.

3 Comments

Are the extra arguments passed to the dispatch method of a class based view?
Thanks Daniel!, but where do I extract the category parameter? In my get method?
Thanks, it can be obtained from kwargs!

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.