Django Python表单提交,从数据库中删除行并刷新页面



我是 Django 的新手,如果有人能帮助我解决这个问题,我将不胜感激。

我在后端有一个数据库,其中包含 100 行用户信息。 姓名,姓氏,电话号码。

数据库在主页模板上可见,如果您选择其中一个名称,则可以向此人捐赠一些东西。

当您单击提交按钮时,将引导您进入新的ajax窗口,您可以在其中输入数据,然后提交。

然后我通过电子邮件收到了您的消息。

我的问题是如何同时确认(提交(并从数据库中删除行(数据库中的人(,然后刷新页面? 意思是,当您提交表单时,功能应该立即从主页中删除人员,并且必须刷新页面,以便您可以看到另一个人?

这是代码。 我将不胜感激任何帮助。 谢谢大家。

views.py

def about(request):
context = {
'num_toys': '1',
}
return render(request, 'about.html')  # , context=context
def couses(request):
db_queryset = Children.objects.all()
context = {'child': db_queryset}
return render(request, 'couses.html', context=context)
class ChildrenListView(ListView):
model = Children
context_object_name = 'child'
class ChildrenCreateView(CreateView):
model = Children
form_class = ChildrenForm
success_url = reverse_lazy('children_changelist')
class ChildrenUpdateView(UpdateView):
model = Children
form_class = ChildrenForm
success_url = reverse_lazy('children_changelist')
class ChildrenDetailView(DetailView):
model = Children
form_class = ChildrenForm
success_url = reverse_lazy('children_detail')

children_detail.html



<!-- Start contact form area -->
	    <div class="couses">		
<section class="contact-form-area pb-60 pt-90">
			<div class="couses">
<div class="container">
<div class="row">
<!-- Start section title -->
<div class="col-sm-12">
<div class="section-title text-center">
							<h2>Donate <span> {{ children.toy }} </span> to <span>{{ children.name }}</span> who is <span>{{children.date }} old</span></h2>
<img src="static/children/img/title-bottom.png" alt="">
</div>
</div>
<!-- End section title -->
<div class="col-sm-12">
<div class="contact-form">
<form id="contact-form" method="POST" action="mail.php">
<div class="form-fields">
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="Your Name" required>
</div>
<div class="form-fields">
<label for="email">Email</label>
<input id="email" name="email" type="text" placeholder="Your Email" required>
</div>
<div class="form-fields last">
<label for="phone">Phone</label>
<input id="phone" name="phone" type="text" placeholder="Your Phone" required>
</div>
<div class="message-fields">
<label for="mess">Message</label>
<textarea name="mess" id="mess" cols="30" rows="10" placeholder="Message"></textarea>
</div>
<div class="form-button">
<button type="submit">Send your message</button>
<button type="reset">Reset</button>
</div>
</form>

<p class="form-messege"></p>
</div>
</div>
</div>
</div>
			</div>
</section>

如果我错了,对不起,但我知道你想做两个动作。

在您的代码中,我可以看到您有窗体和基于类的视图。也许您需要覆盖函数form_valid以执行提交时所需的操作。

查看此网站 http://ccbv.co.uk 在那里您将找到视图的详细信息。

点击提交时,点击网址并首先处理电子邮件部分的消息,然后您可以通过在视图中编写查询来过滤掉该特定人员的对象,从而使用该表的任何主键过滤掉该人员。 然后将该表的剩余数据呈现到您要从提交单击重定向的模板中。

从上面的对话中,我了解到您不想从数据库布尔字段中删除该人将是一个不错的选择,而是您想保存通过电子邮件发送的消息,通过这种方式您可以同时执行这两项操作。 您将消息保存在数据库中,并且从空消息数据可以在模板上呈现这些用户。

最新更新