成功删除、添加或编辑项目的 django 消息
Posted
技术标签:
【中文标题】成功删除、添加或编辑项目的 django 消息【英文标题】:django messages for a successfully delete,add or edit item 【发布时间】:2018-05-18 03:09:05 【问题描述】:我已经开始使用 django 消息来创建项目。 它非常适合创建添加新项目。
但是..我希望每个动作都有功能- 删除、创建、编辑(我每个都有不同的按钮) 我只有发布功能..当我尝试创建项目已成功删除的消息时,我真的很困惑..我怎么知道删除已提交而不是发布?因为一切都通过 post 功能。
PostEdit 和 Delete 没有消息所需的“请求”。
所以现在我只有每次创建服务器时都会运行的 messages.succuess。 我想为删除、编辑、创建和错误提供不同的消息。
有人知道吗?
index.html -
% if messages %
<ul class="messages">
% for message in messages %
<div class="alert alert-success alert-dismissible fade show" role="alert">
<li% if message.tags % class=" message.tags "% endif %> message </li>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
% endfor %
</ul>
% endif %
views.py -
# Create your views here.
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.template import RequestContext
from django.views.generic import TemplateView, UpdateView, DeleteView, CreateView
from DevOpsWeb.forms import HomeForm
from DevOpsWeb.models import serverlist
from django.core.urlresolvers import reverse_lazy
from simple_search import search_filter
from django.db.models import Q
from django.contrib import messages
class HomeView(TemplateView):
template_name = 'serverlist.html'
def get(self, request):
form = HomeForm()
query = request.GET.get("q")
posts = serverlist.objects.all()
forms =
if query:
posts = serverlist.objects.filter(Q(ServerName__icontains=query) | Q(Owner__icontains=query) | Q(Project__icontains=query) | Q(Description__icontains=query) | Q(IP__icontains=query) | Q(ILO__icontains=query) | Q(Rack__icontains=query))
else:
posts = serverlist.objects.all()
for post in posts:
forms[post.id] = HomeForm(instance=post)
args = 'form' : form,'forms': forms, 'posts' : posts
return render(request, self.template_name, args)
def post(self,request):
form = HomeForm(request.POST)
posts = serverlist.objects.all()
forms =
if form.is_valid(): # Checks if validation of the forms passed
post = form.save(commit=False)
post.save()
messages.success(request,'0 has been added successfully!'.format(post.ServerName))
return redirect('serverlist')
messages.error(request,'Servername is required, please refresh the page and try again.')
for post in posts:
forms[post.id] = HomeForm(instance=post)
args = 'form' : form, 'forms': forms, 'posts' : posts
return render(request, self.template_name,args)
class PostDelete(DeleteView):
model = serverlist
success_url = reverse_lazy('serverlist')
class PostEdit(UpdateView):
template_name = 'serverlist.html'
model = serverlist
form_class = HomeForm
#messages.success(request,"The server has been edited successfully")
#fields = ['ServerName','Owner','Project','Description','IP','ILO','Rack','Status']
success_url = reverse_lazy('serverlist')
【问题讨论】:
【参考方案1】:重写deleteview中的delete方法来改变消息类型:
class PostDelete(SuccessMessageMixin, DeleteView):
model = serverlist
success_url = reverse_lazy('serverlist')
success_message = "Object deleted"
def delete(self, request, *args, **kwargs):
messages.warning(self.request, self.success_message)
return super(PostDelete, self).delete(request, *args, **kwargs)
您可以通过覆盖 UpdateView 的更新方法来完成同样的操作。
【讨论】:
效果很好!我现在唯一担心的是我如何将实际的服务器名称放在消息中......在发布功能中我可以使用'0已成功添加!'.format(post.ServerName)) 但在这里我不能使用 post.ServerName.. 另外,你知道如何加粗服务器名吗?因为它不是 html 并且我在 django 中没有 ,? 澄清:对于其他基于类的视图,SuccessMessageMixin
显示 success_message
而无需覆盖 delete
方法。然而,对于DeleteView
s,情况并非如此,这需要被覆盖——参见code.djangoproject.com/ticket/21926。【参考方案2】:
class PostEdit(UpdateView):
template_name = 'serverlist.html'
model = serverlist
form_class = HomeForm
#messages.success(request,"The server has been edited successfully")
你不能像这样在类定义中调用messages.success
。调用必须以post
或form_valid
之类的方法进行。
消息框架有一个SuccessMessageMixin
,您可能会发现它很有用。您将 mixin 添加到每个视图中,并设置 success_message
:
class PostDelete(SuccessMessageMixin, DeleteView):
model = serverlist
success_url = reverse_lazy('serverlist')
success_message = "Object deleted"
有关更多信息,请参阅adding messages to class-based views 上的文档。
【讨论】:
您知道如何像在帖子中那样添加实际的服务器名称吗? post.servername 在这里不起作用,因为该类不知道它是什么 我链接的文档展示了如何做到这一点。如果值在表单数据中,则可以使用字符串插值,也可以覆盖get_success_message
。以上是关于成功删除、添加或编辑项目的 django 消息的主要内容,如果未能解决你的问题,请参考以下文章