gpt4 book ai didi

django - 使用基于类的 View 显示成功的消息

转载 作者:行者123 更新时间:2023-12-03 13:52:17 25 4
gpt4 key购买 nike

我想在保存行时使用Django的消息传递框架和基于类的 View 显示成功消息,并显示以下代码,但不显示消息。

任何帮助将不胜感激

#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 %}

最佳答案

像这样使用self.request:

from django.contrib import messages
messages.add_message(self.request, messages.INFO, 'Hello world.')

关于django - 使用基于类的 View 显示成功的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24914637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com