5

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.

thedeepfield
  • 6,138
  • 25
  • 72
  • 107

1 Answers1

14

Did you add the context processor and the middleware?

Skylar Saveland
  • 11,116
  • 9
  • 75
  • 91
  • I added it to template_context_processors, but when I do it says I also need to add 'django.contrib.auth.context_processors.auth', which I do and then my page css formatting disappears.. after adding those two, it still does not work.. – thedeepfield Aug 06 '12 at 19:54
  • 2
    The docs say "The default settings.py created by django-admin.py startproject already contains all the settings required to enable message functionality" but for some reason mine didn't include the TEMPLATE_CONTEXT_PROCESSORS. So watch out! – Alveoli Aug 08 '14 at 13:58
  • @Alveoli what version are you using? It may be a bug that you are experiencing. – Skylar Saveland Aug 08 '14 at 17:54
  • I experienced a similar problem, my TEMPLATE_CONTEXT_PROCESSORS didn't include django.contrib.messages.context_processors.messages. adding it fixed the issue. thanks! – fang_dejavu Dec 22 '15 at 00:40