Passing variables to a Django generic view template using reverse()
-
22-08-2019 - |
Question
I have a urlpatterns like this:
urlpatterns = patterns('',
...
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict, 'poll_detail'),
...
My html page template contains this:
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
My view code contains:
return HttpResponseRedirect(reverse('poll_detail',args=(p.id,)))
My question is, where in the reverse() call do I place my 'error_message' variable value?
Apologies if this is a stupid question but I have attempted to search the documentation.
Solution
reverse()
converts a view name to a URL. It does not call the view. Also, it can only set arguments in the URL; it cannot set template tags.
What you can do is make error_message
an argument to the poll_detail
view. This way, you can pass its value to the view using reverse()
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow