Reputation: 28662
I'm trying to add messages to a first view and pass them to a second view with a redirect:
First view:
def index(request):
...etc...
messages.info(request, "My message!")
return redirect('second_view', pk=pk)
Second view:
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,
})
return context
@method_decorator(login_required)
@method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
return super(SecondView, self).dispatch(*args, **kwargs)
template.html
:
{% if messages %}
<div>
<p>This is a test.</p>
{% for message in messages %}
{{message}}
{% endfor %}
</div>
{% endif %}
settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'accounts.context_processors.extra_context',
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
.....etc.....
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
)
But in my template.html, {% if messages %}
returns false. How can I get these messages to show up?
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.
EDIT2:
I have a test in which the message appears properly in the html, which passes:
def test_home_page_signup_messages(self):
data = create_signup_post_data()
url = reverse('signup')
response = self.client.post(url, data, follow=True)
messages = [m.message for m in list(response.context['messages'])]
self.assertIn("My message!", messages) # PASSES
self.assertIn("My message!", response.content) # PASSES
Upvotes: 3
Views: 1094
Reputation: 254
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
Upvotes: 0
Reputation: 5388
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>
Upvotes: 2