gpt4 book ai didi

javascript - 成功删除、添加或编辑项目的 django 消息

转载 作者:行者123 更新时间:2023-11-28 14:42:12 24 4
gpt4 key购买 nike

我已经开始使用 django 消息来创建项目。它非常适合创建添加新项目。

但是..我希望每个 Action 都有函数 -删除、创建、编辑(每个按钮都有不同的按钮)我只有发布功能..当我尝试创建一条消息,表明该项目已成功删除时,这真的让我很困惑..我怎么知道删除已提交而不是发布?因为一切都经过 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">&times;</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')

最佳答案

重写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 的更新方法来执行相同的操作。

关于javascript - 成功删除、添加或编辑项目的 django 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636968/

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