gpt4 book ai didi

python - Django DeleteView 不会从数据中删除对象

转载 作者:行者123 更新时间:2023-11-29 16:40:17 29 4
gpt4 key购买 nike

我刚刚学习了 CreateView、UpdateView 和 DeleteView 模型,然后我让它们全部工作起来。我可以更改我的对象,我可以删除它们,但是在删除一个对象后,我尝试创建另一个对象,并创建一个像之前未删除的对象。给我以下之前删除的pk

它仍然创建 de 对象,尽管目前的 pk 应该是 3,我认为在单击删除按钮并确认删除后,它并没有从数据中删除。

这些是我的模型:

from django.db import models from django.urls import reverse

class Dreams (models.Model):
titulo = models.CharField(max_length=100)
objetivo = models.CharField(max_length=100)
imagem = models.CharField(max_length=100)

def get_absolute_url(self):
return reverse ('webdeve:index', kwargs={'pk': self.pk})

def __str__(self):
return self.titulo + ' - ' + self.objetivo


class Which (models.Model):
lets = models.ForeignKey(Dreams, on_delete=models.CASCADE)
make = models.CharField(max_length=100)
it = models.CharField(max_length=100)
favorite = models.BooleanField(default=False)

def __str__(self):
return self.make

我的观点.py:

from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Dreams, Which
from django.urls import reverse_lazy


class IndexView (generic.ListView):
template_name = 'index.html'

def get_queryset(self):
return Dreams.objects.all()

class DetailView (generic.DetailView):
model = Dreams
template_name = 'detail.html'

class DreamCreate (CreateView):
model = Dreams
fields = ['titulo', 'objetivo', 'imagem']
template_name = 'dreams_form.html'


class DreamUpdate (UpdateView):
model = Dreams
fields = ['titulo', 'objetivo', 'imagem']
template_name = 'dreams_form.html'

class DreamDelete (DeleteView):
model = Dreams
template_name= ('dreams_confirm_delete.html')
success_url= reverse_lazy('webdeve:index')

我的urls.py:

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

app_name = 'webdeve'

# Dreams

urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),

# Dreams/detail

url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

# Dreams/detail/add
url(r'^dream/add/$', views.DreamCreate.as_view(), name='dream-add'),

# Dreams/detail/Update

url(r'^dream/(?P<pk>[0-9]+)/$', views.DreamUpdate.as_view(), name='dreams-uptdate'),
# Dreams/detail/detete

url(r'^dream/(?P<pk>[0-9]+)/delete/$', views.DreamDelete.as_view(), name='dreams-delete'),

]

还有我的带有删除按钮的index.html:

<!-- linkando css no html -->
{% extends 'base.html' %}
{% block nav %}

<ul>
{% for Dreams in object_list %}
<a href="{% url 'webdeve:detail' Dreams.id %}"><img src={{ Dreams.imagem }}></a>
<li><a href="{% url 'webdeve:detail' Dreams.id %}"> {{ Dreams.titulo }} - {{ Dreams.objetivo }} </a><li>

<!--delete BUTTON-->

<form action="{% url 'webdeve:dreams-delete' Dreams.id %}">
{% csrf_token %}
<input type="hidden" name="dreams_id" value="{{ Dreams.id }}" method="post" style="display: inline" >
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
{% endfor %}
</ul>
{% endblock %}

最佳答案

DeleteView 仅在 POST 上删除您的对象,而不是在 GET 上删除您的对象。因此,您需要在表单中使用 method="post",因为 GET 会再次呈现此confirm_delete.html。

<form method="post" action="">
...
</form>

如果您可以通过 GET 方式进入 DeleteView,则只需 POST action="" 即可。

关于python - Django DeleteView 不会从数据中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53381223/

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