Вопрос

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?

Это было полезно?

Решение

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')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top