I’m trying to use the message middleware in Django, but the view does not render it at all. What I have:
views.py
messages.success(request, 'Test message.')
return render_to_response('base.html')
template.html
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">{{ message }}</li>
{% endfor %}
</ul>
When I do this in the view:
for message in messages.get_messages(request):
print message
it prints a message for every time I called the view since i last used this function, so it definitely saves the messages somewhere. It is just not sent to the template.
Additional info:
- Django version = (1, 2, 3, ‘final’, 0)
- I added ‘django.contrib.messages.context_processors.messages’ to TEMPLATE_CONTEXT_PROCESSORS. Note: I had no TEMPLATE_CONTEXT_PROCESSORS even though it’s supposed to be there since Django 1.2 so I just added it. I guess that it might not be working properly and that this might be the problem, but I don’t know how to check if it is properly configured.
- I followed https://docs.djangoproject.com/en/dev/ref/contrib/messages/ to set it up.
You need to render the template with a
RequestContextinstead of justContext(whichrender_to_responseuses by default) to make your template context processors available in the template. Check out theoptional argumentssection of therender_to_responsedocs then change: