2010-03-16 69 views
0

我正在Django的1.2測試版,並嘗試新功能的消息框架:消息框架。問題與在Django 1.2

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

一切似乎工作,但是當我試圖輸出消息,我什麼也沒得到。看起來messages變量是空的。我仔細檢查了所有設置,它們看起來就像在手冊中一樣。什麼可能是錯的?

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 %} 

在模板中的N沒有輸出。

回答

5

你需要在你的電話使用RequestContextrender_to_response

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

請參閱註釋this documentation section下的幾個屏幕。

+0

Tkank你!需要添加從django.template導入RequestContext以及 – 2010-03-16 12:02:40