我认为我的设置有问题,当我在登录操作中使用flash时我无法在索引模板中看到它,但当我在“my”操作中使用flash时,我看到重复的消息
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'bookmarks',
)
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',
)
视图:
def login(request):
...
if user is not None and user.is_active:
auth.login(request, user)
messages.add_message(request, messages.SUCCESS, 'cant see it')
return redirect(reverse('bookmarks:index'))
...
def my(request):
...
messages.add_message(request, messages.SUCCESS, 'this is twice')
return render(request, 'bookmarks/my.html', {
'bookmarks': bookmarks
})
和我的index.html
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
// 修改 也许有路由的东西?
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
...
url(r'^my/$', views.my, name = 'my'),
...
)
答案 0 :(得分:0)
这些Flash消息仅在呈现页面时显示。
这就是你看到两条消息的原因: