Add json encoded messages (from Django messages framework) to templates
pip install django-json-messages
Make sure the Django messages framework is installed and working.
Add json_messages
to your list of INSTALLED_APPS
Add json_messages.context_processors.json_messages
in your CONTEXT_PROCESSORS
settings.
A json_messages
property is now available on the context.
It is a dict version of the messages with msg
and type
keys
Use {% load json_messages %}
at the top of your template
Use {% json_messages_dump %}
to simply dump the json encoded messages into your template
Note that all messages' msg
properties are passed through Django's escapejs for security reasons.
Use {% json_messages_script %}
to dump the messages array into a Javascript window variable called messages
This is equivalent to writing
<script type="text/javascript">
window.messages = {% json_messages_dump %};
</script>
- Set a context variable
js_variable
to override the name of the window variable use by{% json_messages_script %}
Example:
{% json_messages_script js_variable="blop" %}
results in
<script type="text/javascript">
window.blop = [{"type":"danger", "msg":"Something"}];
</script>
Defaults to "messages"
None