我需要从数据库中删除现有条目。为了删除我正在使用基于类的视图,并使用方法super()。delete(* args,** kwargs),但是我反复遇到相同的错误..有什么办法可以解决它吗?
class DeletePost(LoginRequiredMixin, generic.DeleteView):
model = models.Post
success_url = reverse_lazy("blog:all")
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user_id=self.request.user.id)
def delete(self, *args, **kwargs):
messages.success(self.request, "Post Deleted")
return super().delete(*args, **kwargs)
User = get_user_model()
class Post(models.Model):
user=models.ForeignKey(User,related_name='posts',on_delete=models.CASCADE)
title = models.CharField(max_length=150, default=('New Post'))
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:all')
class Meta():
ordering = ['-created_at']
unique_together = ('user','message')
{% extends "blog/blog_base.html" %}
{% block post_content %}
<h3>Are you sure you want to delete this post?</h3>
{% include "blog/_blog_detail.html" with post=object hide_delete=True %}
<form method="POST">
{% csrf_token %}
<input type="submit" value="Confirm Delete">
<a href="{% url 'blog:single' username=user.username pk=object.pk %}">Cancel</a>
</form>
{% endblock %}