Search code examples
djangojinja2django-messages

Django messages framework with built-in Jinja2 backend


How can I use the Django messages framework with Jinja2 and the built-in Jinja2 backend in Django 1.8?

I tried doing it as before, but then remembered that the Jinja2 backend doesn't have the context processors of Django Templating Language. Is it possible via the request as with session.

I have been using Django only for a few months, so even if the answer is obvious, please let me know.


Solution

  • In the end I decided to go with the Jinja2 environment. I added a global callable to the environment like 'messages': messages.get_messages, which will let me access it like a context processor, but with messages(request) instead of the object 'messages' in DTL.

    For beginners like me who doesn't know much about Jinja2, I added this callable to the jinja2.py file, which we create for enabling Jinja2 backend, just below the callable for url. You also have to import messages module from django.contrib in this file. Now, you can access the messages store from the Jinja2 template by accessing it using messages(request).