Django Message框架未在模板中显示消息

时间:2013-04-26 12:10:13

标签: python django

这是我的设置文件

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
       "django.core.context_processors.request",
       "django.core.context_processors.media",
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'h^@hi8e&q4e#h!j4v$x+@y2ngs&3&*o%!u8pi(vp3h8n&0$*a)'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (

   'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

在模板中,我添加了以下代码以显示消息

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

在我的观点中,我正在创建像

这样的消息
print results
if results == ():
    messages.success(request, 'This id is not valid key')
    return HttpResponseRedirect('/subscriber/login/')

我已在视图中导入from django.contrib import messages

我不知道我在这里做错了什么。我无法在登录页面中收到任何消息。

请帮帮我!

2 个答案:

答案 0 :(得分:5)

将您的上下文处理器更改为:

TEMPLATE_CONTEXT_PROCESSORS = (
   "django.core.context_processors.request",
   "django.core.context_processors.media",
   "django.contrib.messages.context_processors.messages"
)

这样django会将所有消息放到用于呈现模板的上下文中,因此您可以访问模板中的messages

顺便说一句。您永远不应该在公开场合发布SECRET_KEY。从不。

来源:https://docs.djangoproject.com/en/dev/ref/contrib/messages/

编辑:更改设置摘录OP提供

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
       "django.core.context_processors.request",
       "django.core.context_processors.media",
       "django.contrib.messages.context_processors.messages"
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'h^@hi8e&amp;q4e#h!j4v$x+@y2ngs&amp;3&amp;*o%!u8pi(vp3h8n&amp;0$*a)'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (

   'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

答案 1 :(得分:2)

尝试将django.contrib.messages.context_processors.messages添加到模板上下文处理器列表中。