尽管触发了视图,为什么评论 objects 没有从数据库中删除?
why are comment objects not deleted from database although the view is triggered?
我尝试删除评论,因此,我第一次尝试通过 class-based 视图,然后我对其进行散列以通过正常功能尝试第二种方式,以了解这里发生了什么。所以,当我尝试通过 ID 删除评论时,什么也没有发生,它只是将我引导到网页而不删除评论,所以在这种情况下,程序运行但不删除 object 所以,发生了什么在吗?
注意:同一页面上的 post 和评论以及该页面上的 slug 字段后面是 post 而不是评论。
如果 post 的标题是:新标题 那么,根据 post
,slug 将是 new-title
question_view.html
<div class="user-answer">
<div class="row">
<div class="col-xs-12">
{% for comment in my_question.comment_set.all %}
<div class="comments">
<div class="col-xs-0">
<div class="avatar">
<a href="{% url 'account:view_profile' comment.username %}">
<img class="img-circle img-thumbnail" style="width:50px; height: 50px;" src="{{ comment.logo }}">
</a>
</div>
</div>
<div class="col-xs-10">
<!-- --Comment itself-- -->
<div class="user_comment">
<p>{{ comment }}</p>
<div class="fa fa-caret-left comment-arrow"></div>
</div>
<!-- start Options in comment -->
<div class="sub-options">
{% if request.user.username == comment.username %}
<!-- --Edit comment-- -->
<div class="edit-comment">
<a>Edit</a>
</div>
<!-- --Delete comment-- -->
<div class="delete-comment">
<form method="post" action="{% url 'community:delete_comment' comment.pk %}">
{% csrf_token %}
<input type="hidden" name="delete-comment" value="{{ comment.comment }}">
<input type="submit" value="delete">
</form>
</div>
{% endif %}
<!-- end Options in comment -->
<!-- --comment Date-- -->
<div style="display: inline-block;color: #8e8e8e" class="comment-date">
<p>{{ comment.date|time }}</p>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
{% endfor %}
</div>
</div>
</div>
views.py
# Delete post
# class DeleteComment(DeleteView, SingleObjectMixin):
# model = Comment
# pk_url_kwarg = 'pk'
# template_name = 'community/question_view.html'
# queryset = Comment.objects.all()
#
# def get_success_url(self):
# title = UserAsking.objects.get(title=self.object)
# slug = UserAsking.objects.get(title=title).ask_slug
# return reverse_lazy('community:question_view', kwargs={'user_slug': slug})
#
# def get_object(self, queryset=None):
# request_comment = self.request.POST['delete-comment']
# return self.get_queryset().filter(pk=request_comment).get()
def delete_comment(request, pk):
if request.method == 'POST':
comment = Comment.objects.get(pk=pk)
del comment
return redirect('community:user_questions')
urls.py
urlpatterns = [
# path('delete-comment/<int:pk>/', views.DeleteComment.as_view(), name="delete_comment"),
path('delete-comment/<int:pk>/', views.delete_comment, name="delete_comment"),
]
models.py
class Comment(models.Model):
userasking = models.ForeignKey(UserAsking, on_delete=models.CASCADE)
comment = models.TextField(max_length=500, blank=True)
date = models.DateTimeField(auto_now_add=True)
userprofile = models.ForeignKey(UserProfile, on_delete=models.CASCADE, default=1)
name = models.CharField(max_length=30, blank=False, default='empty')
logo = models.ImageField(upload_to='images/', default='images/default-logo.jpg', blank=True)
username = models.CharField(max_length=30, blank=False, default='empty')
def __str__(self):
return self.comment
我希望如果你能通过 class based-view 解释发生了什么,我将不胜感激。
请注意:如果你能准确理解发生了什么,你就会知道我没有看到任何错误,因此,我没有得到任何异常或回溯。提前谢谢你。
试试这个:
def delete_comment(request, pk):
if request.method == 'POST':
comment = Comment.objects.get(pk=pk).delete()
return redirect('community:user_questions')
您的函数仅删除对象而不删除数据库条目,因为您没有触发有效的 SQL 操作(在本例中为删除)。
您只是再次删除了先前分配的对象comment
,但不影响数据库条目:
def delete_comment(request, pk):
if request.method == 'POST':
comment = Comment.objects.get(pk=pk)
del comment
return redirect('community:user_questions')
更多关于 delete()
的官方文档:
更新您的评论:
您不能使用 Python 的 del
语句与 Django 中的数据库条目进行交互。
Pythons del
语句用于删除最初由Python创建的对象,如列表、变量等。
为了在 Django 中与数据库交互,您必须使用 Django 内置的工具箱 Model instance,它基本上将原始 SQL 操作转换为易于使用的方法。
因此,也许您可以通过调用数据库“对象”条目和python对象来调整适当的措辞以突出差异:好吧,对象..
但是,Django 仍然提供使用 raw SQL 作为回退的选项。
我尝试删除评论,因此,我第一次尝试通过 class-based 视图,然后我对其进行散列以通过正常功能尝试第二种方式,以了解这里发生了什么。所以,当我尝试通过 ID 删除评论时,什么也没有发生,它只是将我引导到网页而不删除评论,所以在这种情况下,程序运行但不删除 object 所以,发生了什么在吗?
注意:同一页面上的 post 和评论以及该页面上的 slug 字段后面是 post 而不是评论。
如果 post 的标题是:新标题 那么,根据 post
question_view.html
<div class="user-answer">
<div class="row">
<div class="col-xs-12">
{% for comment in my_question.comment_set.all %}
<div class="comments">
<div class="col-xs-0">
<div class="avatar">
<a href="{% url 'account:view_profile' comment.username %}">
<img class="img-circle img-thumbnail" style="width:50px; height: 50px;" src="{{ comment.logo }}">
</a>
</div>
</div>
<div class="col-xs-10">
<!-- --Comment itself-- -->
<div class="user_comment">
<p>{{ comment }}</p>
<div class="fa fa-caret-left comment-arrow"></div>
</div>
<!-- start Options in comment -->
<div class="sub-options">
{% if request.user.username == comment.username %}
<!-- --Edit comment-- -->
<div class="edit-comment">
<a>Edit</a>
</div>
<!-- --Delete comment-- -->
<div class="delete-comment">
<form method="post" action="{% url 'community:delete_comment' comment.pk %}">
{% csrf_token %}
<input type="hidden" name="delete-comment" value="{{ comment.comment }}">
<input type="submit" value="delete">
</form>
</div>
{% endif %}
<!-- end Options in comment -->
<!-- --comment Date-- -->
<div style="display: inline-block;color: #8e8e8e" class="comment-date">
<p>{{ comment.date|time }}</p>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
{% endfor %}
</div>
</div>
</div>
views.py
# Delete post
# class DeleteComment(DeleteView, SingleObjectMixin):
# model = Comment
# pk_url_kwarg = 'pk'
# template_name = 'community/question_view.html'
# queryset = Comment.objects.all()
#
# def get_success_url(self):
# title = UserAsking.objects.get(title=self.object)
# slug = UserAsking.objects.get(title=title).ask_slug
# return reverse_lazy('community:question_view', kwargs={'user_slug': slug})
#
# def get_object(self, queryset=None):
# request_comment = self.request.POST['delete-comment']
# return self.get_queryset().filter(pk=request_comment).get()
def delete_comment(request, pk):
if request.method == 'POST':
comment = Comment.objects.get(pk=pk)
del comment
return redirect('community:user_questions')
urls.py
urlpatterns = [
# path('delete-comment/<int:pk>/', views.DeleteComment.as_view(), name="delete_comment"),
path('delete-comment/<int:pk>/', views.delete_comment, name="delete_comment"),
]
models.py
class Comment(models.Model):
userasking = models.ForeignKey(UserAsking, on_delete=models.CASCADE)
comment = models.TextField(max_length=500, blank=True)
date = models.DateTimeField(auto_now_add=True)
userprofile = models.ForeignKey(UserProfile, on_delete=models.CASCADE, default=1)
name = models.CharField(max_length=30, blank=False, default='empty')
logo = models.ImageField(upload_to='images/', default='images/default-logo.jpg', blank=True)
username = models.CharField(max_length=30, blank=False, default='empty')
def __str__(self):
return self.comment
我希望如果你能通过 class based-view 解释发生了什么,我将不胜感激。
请注意:如果你能准确理解发生了什么,你就会知道我没有看到任何错误,因此,我没有得到任何异常或回溯。提前谢谢你。
试试这个:
def delete_comment(request, pk):
if request.method == 'POST':
comment = Comment.objects.get(pk=pk).delete()
return redirect('community:user_questions')
您的函数仅删除对象而不删除数据库条目,因为您没有触发有效的 SQL 操作(在本例中为删除)。
您只是再次删除了先前分配的对象comment
,但不影响数据库条目:
def delete_comment(request, pk):
if request.method == 'POST':
comment = Comment.objects.get(pk=pk)
del comment
return redirect('community:user_questions')
更多关于 delete()
的官方文档:
更新您的评论:
您不能使用 Python 的 del
语句与 Django 中的数据库条目进行交互。
Pythons del
语句用于删除最初由Python创建的对象,如列表、变量等。
为了在 Django 中与数据库交互,您必须使用 Django 内置的工具箱 Model instance,它基本上将原始 SQL 操作转换为易于使用的方法。
因此,也许您可以通过调用数据库“对象”条目和python对象来调整适当的措辞以突出差异:好吧,对象..
但是,Django 仍然提供使用 raw SQL 作为回退的选项。