Django messages framework with built-in Jinja2 backend -
how can use django messages framework jinja2 , built-in jinja2 backend in django 1.8?
i tried doing before, remembered jinja2 backend doesn't have context processors of django templating language. possible via request session.
i have been using django few months, if answer obvious, please let me know.
to expand bit on answer above, here's step-by-step breakdown.
first, enable custom environment jinja2, as described here
in settings.py, point environment option jinja2 function
`templates = [ { "backend": "django_jinja.backend.jinja2", "app_dirs": true, "options": { "match_extension": ".jinja", "environment": "myapp.jinjaconfig.environment", } }, ...]`
now write function add messages environment. create myapp/jinjaconfig.py (or whatever name choose, match added settings.py):
from jinja2 import environment django.contrib import messages def environment(**options): env = environment(**options) env.globals.update({ 'get_messages': messages.get_messages, }) return env
at point have get_messages available in template. can use this:
{% message in get_messages(request) %} <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> {% endfor %}
note have pass in request argument there
Comments
Post a Comment