[Django]-Django 1.7.4: messages not showing up in TemplateView

2👍

I believe that the issue here is that the redirect does not pass any context, which would contain the message. The documentation explains that:

If you’re using the context processor, your template should be
rendered with a RequestContext. Otherwise, ensure messages is
available to the template context.

Thus, messages will not available after a redirect. However, you can retrieve messages yourself with django.contrib.messages.get_messages:

from django.contrib.messages import get_messages

class SecondView(TemplateView):
    template_name = "template.html"                                                     

    def get_context_data(self, **kwargs):                                           
        context = super(SecondView, self).get_context_data(**kwargs)                  
        pk = kwargs.get('pk')                                                                                                
        context.update({                                                            
            'pk': pk,
            'messages': get_messages(self.request),                                                                                                               
        })                                                                          
        return context                                                              

    @method_decorator(login_required)                                               
    @method_decorator(ensure_csrf_cookie)                                           
    def dispatch(self, *args, **kwargs):                                            
        return super(SecondView, self).dispatch(*args, **kwargs) 

In response to:

EDIT:

If I simply try to put {{messages}} into my template, it renders like this: <django.contrib.messages.storage.fallback.FallbackStorage object at 0x108271950>

but when I call {% if messages %} this does not return the contents of the {%if%} statement.

This is my exact code, less the URLs:

# ------- URLS
# Django
from django.conf.urls import patterns, url

from .views import SecondView

urlpatterns = patterns("myapp.views",
    url(r'^test/$', 'index'),
    url(r'^test/(?P<pk>\d+)/$', view=SecondView.as_view(), name='second_view')
)

# ------- VIEWS

from django.views.generic import TemplateView
def index(request):
    from django.contrib import messages
    from django.shortcuts import redirect
    messages.info(request, "My message!")
    pk = 2
    return redirect('second_view', pk=pk)

from django.contrib.messages import get_messages
from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
class SecondView(TemplateView):
    template_name = "template.html"

    def get_context_data(self, **kwargs):
        context = super(SecondView, self).get_context_data(**kwargs)
        pk = kwargs.get('pk')
        context.update({
            'pk': pk,
            'messages': get_messages(self.request),
        })
        return context

    @method_decorator(login_required)
    @method_decorator(ensure_csrf_cookie)
    def dispatch(self, *args, **kwargs):
        return super(SecondView, self).dispatch(*args, **kwargs)

# ------- TEMPLATE
<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        {% if messages %}
            <ul class="messages">
                {% for message in messages %}
                <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
                    {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}Important: {% endif %}
                    {{ message }}
                </li>
                {% endfor %}
            </ul>
        {% endif %}
    </body>
</html>

0👍

Do you have the right import? Which django version are you using? This works for me on v1.7.4:

from django.contrib import messages
messages.add_message(request, messages.SUCCESS,"My message!")

Download my project to see the order of middleware you would need:
https://www.dropbox.com/s/qo601h7xjlk0ztu/project_for_messages.tgz?dl=0

Created from here https://github.com/jpadilla/django-project-template

Leave a comment