如何删除答案(正文中的代码)?
Posted
技术标签:
【中文标题】如何删除答案(正文中的代码)?【英文标题】:How can I delete the answers (code in body)? 【发布时间】:2020-11-07 21:09:54 【问题描述】:我正在创建一个用于练习的问答网站,我创建了答案和问题模型并将它们链接在一起,但是我无法访问我为删除答案模型而设置的模板,我创建了一个 DeleteView
来删除问题。代码如下:
views.py:
class Politics_post_details(DeleteView):
model = PoliticsPost
context_object_name = 'politicsposts'
pk_url_kwarg = 'qid'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# now you can get any additional information you want from other models
question = get_object_or_404(PoliticsPost, pk=self.kwargs.get('qid'))
context['answers'] = Answer.objects.filter(post=question).order_by('-date_posted')
return context
class AnswerDelete(UserPassesTestMixin,DetailView):
model = Answer
success_url = reverse_lazy('Lisk home')
pk_url_kwarg = 'aid'
def test_func(self):
answer = self.get_object()
if self.request.user ==answer.author:
return True
return False
urls.py(不是root):
path('politicspost/<int:qid>/createanswer/',views.CreateAnswer.as_view(template_name='lisk_templates/createanswer.html'),name = 'Answer'),
path('politicspost/<int:qid>/answer/<int:aid>/delete/',views.AnswerDelete.as_view(template_name = 'lisk_templates/answer_delete.html'),name='Answer_Delete'),
path('politicspost/<int:qid>/',views.Politics_post_details.as_view(template_name='lisk_templates/politics_post_details.html'),
我创建了模板,但每当我尝试访问它时,都会出现如下错误:
NoReverseMatch at /politicspost/29/
Reverse for 'Answer_Delete' with arguments '(36,)' not found. 1 pattern(s) tried: ['politicspost/(?P<qid>[0-9]+)/answer/(?P<aid>[0-9]+)/delete/$']
提前致谢。
answer_delete.html:
%extends "lisk_templates/base.html"%
% block title %
Page title
% endblock title %
% block body%
<div class="feed" style="background-color:lightred;"><form method="POST">
% csrf_token %
<h3>Are you sure that you want to delete this answer: <br>
object.content </h3>
<button id="signin" type="submit">Yes</button> <a href="% url 'politics_post_details' object.id %" style="margin-left:10px;"> No</a>
</form>
</div>
% endblock body %
【问题讨论】:
【参考方案1】:你有很多问题。最大的一个是你切换了DeleteView
和DetailView
(你的DeleteView
是DetailView
,反之亦然)。
然后您应该将您的模板重命名为answer_confirm_delete
或将template_name_suffix
添加到您的DeleteView
。有关详细信息,请参阅文档:
https://docs.djangoproject.com/en/3.0/ref/class-based-views/generic-editing/#django.views.generic.edit.DeleteView
如果您使用 Django 的 DeleteView,则无需在模板中指定 url,它会为您完成所有工作。只需确保您在urls.py
中正确指定了网址。修复这些问题,然后看看它是否有效。
【讨论】:
在刚刚创建的模板中,如果用户决定不删除答案,该 URL 将重定向用户。我添加了我已经添加了模板名称后缀,但是在 urls.py 中,我更正了删除视图,这不是问题。 @MeL【参考方案2】:这可能对你有用,因为你从未使用过 DeleteView 的导入
from django.views.generic import DeleteView
"""This Is From My Blog App, That I Used For My Blog App, Hope This Will Help"""
class PostDeleteView(LoginRequiredMixin, DeleteView):
model = Post
success_url = reverse_lazy('post_list')
编码愉快,如果有任何问题,请告诉我我支持你!
【讨论】:
感谢您指出这一点,虽然这个愚蠢的错误会导致很多问题,但这不是这个特定问题的原因,我仍然遇到同样的错误,感谢您再次指出这一点,我觉得自己好傻。 @Aryan Mishra以上是关于如何删除答案(正文中的代码)?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 RequestSpecification/FilterableRequestSpecification 正文中删除属性?