Question

here's a conundrum for you,

Using Django 1.4, I cannot get messages set through the messages middleware to display in my templates. I have combed through the Django docs and ensured that my settings.py file referenced the relevant apps, context-processors and middleware. I have ensured that my view is rendering with the RequestContext instance. Yet, I still cannot get any of the messages to appear in the template.

settings.py:

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
...
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
'tekextensions.context_processors.admin_media_prefix',
)
...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin', #Admin interface
'django.contrib.admindocs', #Admin docs
...

My messages error_level is set to 20 (i.e. 'info' and above). I am using the default mappings.

views.py:

from django.contrib import messages

def index(request, *args, **kwargs):
    #Do some funky jazz which works like build the timeline & page vars
    ...
    messages.error = (request,"Horsey Bollox!")
    messages.add_message = (request, messages.ERROR,"Why won't this f***ing thing work?") #Attempting alternate method
    return render_to_response('funkyjazzdirectory/index.html',
                        {
                            'page': page, 
                            'timeline': timeline,
                        },
                        context_instance=RequestContext(request))

template: (funkyjazzdirectory/index.html)

{% extends "base.html" %}
{% if messages.error %}
    <div class="messages-errors">
        Error:
        <ul>
            {% for msg in messages %}
                <li>{{msg}}</li>
            {% endfor %}
        </ul>
    </div>
{% endif %}
<p>
    Other stuff such as iterating through {{timeline}} which renders absolutely fine
</p>

I have also tried substituting {{msg}} with:

<li>{{msg.message}}</li>

with no success.

The rest of the page outputs fine, Django does not throw an error. The console does not contain anything abnormal. The HTML code produced does not contain the div, nor the list tags anywhere. The template which this one extends (base.html) does not use the {{messages}} variable nor call a template tag which uses it.

I have tried passing the {{messages}} into a custom template tag for testing in the top of the index.html template. Here I can do:

def __init__(self, messages):
    self.messages = messages
    def render(self, context):
    l = dir(context[self.messages])
        print(l)

...which produces a list of methods / properties presumably of the message object. Yet, I cannot iterate over this at all, as "for m in messages:" does not run even once. Attempting to discover the size of this entity by:

print(len(context[self.messages]))

gives me nothing in the console.

The only time I've got it to actually output anything was when I manually passed the messages object into the template within the render_to_response tag then iterated over messages.error ({% for msg in messages.error %}) which produced two bullet points in the correct div: the first being filled with what looks like a var dump: ", POST:, COOKIES:{'csrftoken':"... the second bullet point containing just the last error message: "Why won't this f***ing thing work?". (Obviously this was just a test and I have not kept messages in the dict passed via render_to_response as I know it should arrive in the template via the context)

So, where did I go wrong?

Why can I not see my error messages in my template? Why can I not even get the messages to appear in the console?

Any light someone smarter than me can shed would be extremely helpful!

Was it helpful?

Solution

You seem to use a very strange way to add a message:

messages.error = (request,"Horsey Bollox!")
messages.add_message = (request, messages.ERROR,"Why won't this f***ing thing work?") 

The correct syntax is:

messages.error(request,"Horsey Bollox!")
messages.add_message(request, messages.ERROR,"Why won't this f***ing thing work?") 

The settings and templates are however fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top