我正在尝试在保存表单后显示成功消息。我正在使用message framework来实现此目标
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'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',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_PATH],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
views.py
class VolunteerCreateView(SuccessMessageMixin, CreateView):
model = Volunteer
form_class = VolunteerForm
template_name = 'volunteer.html'
success_message = 'Thank you!'
success_url = reverse_lazy('volunteer')
volunteer.html
{% if messages %}
<div class="alert center block" role="alert">
<ul class="messages">
{% for message in messages %}
<li class="text-center"{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}.....
但是没有显示该消息。我可以在重定向的URL中看到请求标头上的消息json但不显示。 请求标题的一部分
Cookie:olfsk=olfsk1757071755791586; hblid=HecPjm2lDQ2xTzgn3m39N0J00JEB6o1A; sessionid=z8zh4t13mw655mf3yn1vfnow8y58zxt9; csrftoken=GZxsdPZmBsefPU0nv7ZEfEaxsnPCMjIehpiFihY3GtTrhFnuFc5c3NXplD35Qslw; ext_name=jaehkpjddfdgiiefcnhahapilbejohhj;
messages="ab8f5c6976dd0939d4f1b867ca564577ba9cc0d7$[[\"__json_message\"\0540\05425\054\"Thank you!\"]]"
可能是未显示消息的原因。我有什么遗漏。