gpt4 book ai didi

python - 如何在Django中制作删除按钮

转载 作者:行者123 更新时间:2023-12-05 08:14:54 26 4
gpt4 key购买 nike

请帮助我了解如何制作删除按钮,它必须删除一个 Cat

class Cat(models.Model):
class Meta():
db_table = "cat"

paw = models.IntegerField(default=4)
name = models.CharField(max_length=30, null=False, default='Cat')
age = models.IntegerField(default=False, null=False)
species = models.CharField(max_length=50, blank=True)
hairiness = models.IntegerField(default=False, null=False)

def __str__(self):
return self.name

这是我的views.py,希望对您有所帮助。 (这是我星期一工作面试的需要)

from django.shortcuts import render, get_object_or_404
from .models import Cat
from .forms import CatForm
from django.shortcuts import redirect

def home(request):
template = "base.html"
queryset = Cat.objects.all()
context = {
"object_list": queryset
}
return render(request, template, context)

def new_cat(request):
if request.method == "POST":
form = CatForm(request.POST)
if form.is_valid():
cat = form.save(commit=False)
cat.save()
return redirect('/', pk=cat.pk)
else:
form = CatForm()
return render(request, 'new_cat.html', {'form': form})

def cat_edit(request, pk):
cat = get_object_or_404(Cat, pk=pk)
if request.method == "POST":
form = CatForm(request.POST, instance=cat)
if form.is_valid():
cat = form.save(commit=False)
cat.save()
return redirect('/', pk=cat.pk)
else:
form = CatForm(instance=cat)
return render(request, 'new_cat.html', {'form': form})

站点是 asc 以添加更多详细信息,但我只是不知道还能添加什么。

from django.conf.urls import url
from . import views


urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^new/$', views.new_cat, name='new_cat'),
url(r'^edit/(?P<pk>[0-9]+)/$', views.cat_edit, name='cat_edit'),
]

最佳答案

首先,您应该创建一个 cat_delete View ,它应该看起来像这样:

def cat_delete(request, pk):
cat = get_object_or_404(Cat, pk=pk) # Get your current cat

if request.method == 'POST': # If method is POST,
cat.delete() # delete the cat.
return redirect('/') # Finally, redirect to the homepage.

return render(request, 'template_name.html', {'cat': cat})
# If method is not POST, render the default template.
# *Note*: Replace 'template_name.html' with your corresponding template name.

然后,您应该将此 View 映射到您的 urls.py 中:

from django.conf.urls import url
from . import views

app_name = 'cats'
# Note that app_name is added here!
# It is used as a namespace in order to reverse your urls better.
# See usage in template.

urlpatterns = [
# ...
url(r'^delete/(?P<pk>[0-9]+)/$', views.cat_delete, name='cat_delete')
]

在您的模板中,您应该创建一个带有删除按钮 的表单,它只会向删除 View 发送一个POST 请求:

<form action="{% url 'cats:cat_delete' cat.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete cat">
</form>

仔细观察表单的操作:

{% url 'cats:cat_delete' cat.id %}

我在这里使用 app_name来自 urls.py我之前添加的是为了按名称而不是路径解析您的网址。现在cats:cat_delete将评估为 cats/delete/<pk> .当然你通过了cat.id .

这应该可以解决删除 Cat 实例的问题模型。希望我有所帮助。

关于python - 如何在Django中制作删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003056/

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