这是我的设置文件
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',
)
Run Code Online (Sandbox Code Playgroud)
在模板中,我添加了以下代码以显示消息
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
在我看来,我正在创造像
print results
if results == ():
messages.success(request, 'This id is not valid key')
return HttpResponseRedirect('/subscriber/login/')
Run Code Online (Sandbox Code Playgroud)
我已经from django.contrib import messages 在视图中导入了
我不知道我在这里做错了什么.我无法在登录页面中收到任何消息.
请帮帮我!
将您的上下文处理器更改为
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages"
)
Run Code Online (Sandbox Code Playgroud)
这样,django会将所有消息放到用于呈现模板的上下文中,因此您可以访问messages模板.
BTW.你永远不应该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&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',
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2437 次 |
| 最近记录: |