我希望在保存行时显示成功的消息,使用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 %}
答案 0 :(得分:3)
只需使用self.request
,就像这样:
from django.contrib import messages
messages.add_message(self.request, messages.INFO, 'Hello world.')
答案 1 :(得分:1)
#Using django 3.2
#In views.py
from django.contrib import messages
from .models import CreateEmployer
from django.views.generic import CreateView
class SignUpView(CreateView):
model = CreateEmployer
template_name = ‘employee/register_employee.html’
fields = '__all__'
# this method will enable your message to display
# you can also use it to overwrite form data.
def form_valid(self, form):
messages.success(self.request, f”Account created successfully”)
return super().form_valid(form)
#in your urls.py
from .views import SingUpView
from django.urls import path
urlpatterns = [
path(‘register/‘, SignUpView.as_view(), name=“user-register"),
]
#in your template
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{message.tags}}">
{{message}}
</div>
{% endfor %}
{% endif %}
#I hope this helps anyone facing same problem in the future.
答案 2 :(得分:0)
您必须在settings.py
中设置邮件存储空间MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
问候
答案 3 :(得分:0)
使用Django 2.2,根据文档here,这是在基于CLASS的视图上对我自己有用的内容:
settings.py
INSTALLED_APPS = [
'django.contrib.messages',
]
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
TEMPLATES = [
{
'BACKEND': #,
'DIRS': #,
'APP_DIRS': #,
'OPTIONS': {
'context_processors': [
'django.contrib.messages.context_processors.messages',
],
},
]
“默认存储后端取决于会话。这就是为什么必须启用SessionMiddleware并使其出现在MIDDLEWARE中的MessageMiddleware之前。”
views.py
from django.contrib.messages.views import SuccessMessageMixin
class SignUpView(SuccessMessageMixin, CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('users:login')
template_name = 'users/register.html'
success_message = "%(username)s was created successfully"
urls.py
path(_('register/'), views.SignUpView.as_view(), name='register'),
在模板中: (使用引导程序类)
{% if messages %}
{% for message in messages %}
<div class="text-center alert alert-{{ message.tags }}">
{{ message|safe }}
</div>
{% endfor %}
{% endif %}