问题
I'm trying to use django messages framework to display a message when a user signs out of my application. I'm new to django and the documentation isn't very clear to me. Why is my message not showing up?
https://docs.djangoproject.com/en/dev/ref/contrib/messages/#adding-a-message
VIEW.PY
from django.contrib import messages
def signout(request):
logout(request)
messages.add_message(request, messages.INFO, 'Signout Successful.')
return HttpResponseRedirect(reverse(index))
def index(request):
lf = LoginForm()
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)
return render_to_response('test/home.html', {'login_form': lf,}, context_instance=RequestContext(request))
TEMPLATE - index
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
I'm using django1.3. And the following is required (note that .tz is commented out)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
#"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages")
From documentation on TEMPLATE_CONTEXT_PROCESSORS:
New in Django 1.3: The django.core.context_processors.static context processor was added in this release.
New in Django 1.4: The django.core.context_processors.tz context processor was added in this release.
回答1:
Did you add the context processor and the middleware?
来源:https://stackoverflow.com/questions/11834174/django-messages-not-showing