Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django中创建删除按钮_Python_Django - Fatal编程技术网

Python 如何在Django中创建删除按钮

Python 如何在Django中创建删除按钮,python,django,Python,Django,请帮助我了解如何使删除按钮,它必须删除一只猫 这是我的观点。希望你能帮忙。这是我生命的需要 周一的工作面试 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" q

请帮助我了解如何使删除按钮,它必须删除一只猫

这是我的观点。希望你能帮忙。这是我生命的需要 周一的工作面试

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视图,它应该如下所示:

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.
然后,您应该在URL.py中映射此视图:

在这里,我使用我之前添加的app_name from urls.py,以便按名称而不是路径解析URL。现在cats:cat_delete将计算为cats/delete/。当然,你要通过猫证


这应该可以删除您的Cat模型的实例。希望我能帮上忙。

首先,您应该创建一个cat\u delete视图,该视图应该如下所示:

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.
然后,您应该在URL.py中映射此视图:

在这里,我使用我之前添加的app_name from urls.py,以便按名称而不是路径解析URL。现在cats:cat_delete将计算为cats/delete/。当然,你要通过猫证


这应该可以删除您的Cat模型的实例。希望我能帮上忙。

你可以尝试做与你的猫一样的编辑,显示你的视图,请尝试做与你的猫一样的编辑,显示你的视图,请非常感谢,感谢你的帮助和非常重要的回答,现在我理解我的错误非常感谢你的帮助和非常重要的回答,现在我理解我的错误了
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')
]
<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 %}