开发者

Problem with messages framework in Django 1.2

开发者 https://www.devze.com 2022-12-23 15:48 出处:网络
I\'m running Django 1.2 beta and trying out the new feature: message framework. http://docs.djangoproject.com/en/dev/ref/contrib/messages/

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');   

    retu开发者_开发百科rn render_to_response('mytemplate.html', locals()); 

mytemplate.html

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

In template nothing is outputted.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号