To expand a bit on the answer above, here's the step-by-step breakdown.
First, enable a custom Environment for jinja2, as described here
In settings.py, point the environment option for jinja2 to some function
`TEMPLATES = [
{
"BACKEND": "django_jinja.backend.Jinja2",
"APP_DIRS": True,
"OPTIONS": {
"match_extension": ".jinja",
"environment": "myapp.jinjaconfig.environment",
}
},
...]`
Now you write that function to add messages to the environment. Create myapp/jinjaconfig.py (or whatever name you choose, to match what you added to settings.py):
from jinja2 import Environment
from django.contrib import messages
def environment(**options):
env = Environment(**options)
env.globals.update({
'get_messages': messages.get_messages,
})
return env
At this point you have get_messages available in your template. You can use it like this:
{% for message in get_messages(request) %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
Note that you have to pass in the request as an argument there