0

I'm running Django 1.2 beta and trying out the new feature: message framework.

http://docs.djangoproject.com/en/dev/ref/contrib/messages/

Everything seems to work, but when I try to output the messages, I get nothing. Seems that messages variable is empty. I double checked all the settings, they seem to be just like in the manual. What could be wrong?

settings.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware', #send messages to users
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',

    #debug tool
    'debug_toolbar.middleware.DebugToolbarMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.messages.context_processors.messages', #send messages to users
    'django.core.context_processors.auth',
)

#Store messages in sessions
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage';

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    #'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.messages', 
    'debug_toolbar',

    #my apps
    #...
)

views.py

def myview(request):

    from django.contrib import messages

    messages.error(request, 'error test');   
    messages.success(request, 'success test');   

    return render_to_response('mytemplate.html', locals()); 

mytemplate.html

{% for message in messages %}
        {{ message }}<br />
{% endfor %}

In template nothing is outputted.

1 Answer 1

5

You'll need to use a RequestContext in your call to render_to_response.

return render_to_response('mytemplate.html', locals(),
                          context_instance=RequestContext(request))

See the note a few screens down under this documentation section.

Sign up to request clarification or add additional context in comments.

1 Comment

Tkank you! Needed to add from django.template import RequestContext as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.