我想在保存一行时显示一条成功的消息,使用 Django 的消息传递框架和基于类的视图,代码如下所示,但不显示消息。
任何帮助将不胜感激
#views.py
from django.views.generic import ListView, CreateView, UpdateView, TemplateView
from django.contrib.messages.views import SuccessMessageMixin
class CreateEmployee(SuccessMessageMixin, CreateView):
model = Employee
template_name = 'employees/create.html'
form_class = frmCreate
def get_success_url(self):
return reverse('Employees:Create')
def get_context_data(self, **kwargs):
contexto = super(CreateEmployee, self).get_context_data(**kwargs)
contexto['action'] = reverse('Employees:Create')
return contexto
success_message = 'Employee successful created'
#template
#create.html
<form action="{{ action }}" method="POST" role="form">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" class="btn btn-success">
<form>
{% if messages %}
<div class="col-lg-3 color03">
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}