2

这是我的设置文件

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 = '*******************************'
# 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 在视图中导入了

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

请帮帮我!

4

2 回答 2

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',
)
于 2013-04-26T12:14:50.577 回答
2

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

于 2013-04-26T12:13:58.277 回答