5

I'm trying to do something like:

in urls.py:

...
url(r'^(?P<pk>\d+)/$', VideoDetailView.as_view(), name='video_detail', kwargs={'foo:''})
...

in views.py

..
HttpResponseRedirect(reverse('video_detail', kwargs={'pk': id, 'foo':'bar'}))
...

But this doesn't seem to work. I get a Reverse for 'video_detail' with arguments '()' and keyword arguments '{'pk': 13240L, 'foo': 'bar}' not found.

However this does work:

....
HttpResponseRedirect(reverse('video_detail', kwargs={'pk': id}))
...

ie. removing foo: bar from the reverse call. What's the correct way to do this and pass in extra arguments in the reverse url?

1 Answer 1

8

reverse is a function that creates URL.

Because You have specified only pk pattern in your URL patterns, you can only use pk as argument to reverse (it really would not make sense to add foo since the generated url would be exactly the same for any foo value). You can add foo to URL pattern or create multiple named urls, ie:

url(r'^(?P<pk>\d+)/$', VideoDetailView.as_view(), name='video_detail', kwargs={'foo':''})
url(r'^(?P<pk>\d+)/$', VideoDetailView.as_view(), name='video_detail2', kwargs={'foo':'bar'})

or

url(r'^(?P<pk>\d+)/(?P<foo>\w+)/$', VideoDetailView.as_view(), name='video_detail')
Sign up to request clarification or add additional context in comments.

Comments

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.