如何删除答案(正文中的代码)? [英] How can I delete the answers (code in body)?
问题描述
我正在创建一个实践问答网站,我创建了答案和问题模型并将它们链接在一起,但是我无法访问为删除答案模型而设置的模板,因此创建了 DeleteView
删除问题。以下是代码:
I am creating a Q&A website for practice, I created the answer and the question model and linked them together, however I can not access the template that I set for the deletion of the answer model, I created a DeleteView
to delete the question. Here is the code:
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'),
我创建了模板,但是只要尝试要访问它,它给我一个错误,如下所示:
I created the template but whenever I try to access it, it gives me an error as follows:
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:
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 %}
推荐答案
您有很多问题。最大的一个是您已切换 DeleteView
和 DetailView
(您的 DeleteView
是 DetailView
,反之亦然)。
You have a number of issues. The biggest one is that you have switched DeleteView
and DetailView
(your DeleteView
is a DetailView
and vice versa).
然后您应该将模板重命名为 answer_confirm_delete
或将 template_name_suffix
添加到您的 DeleteView
中。请参阅文档以获取更多详细信息:
And then you should either rename your template to answer_confirm_delete
or add the template_name_suffix
to your DeleteView
. See the documentation for further details:
如果您使用Django的DeleteView,则无需在模板中指定url,它将为您完成所有工作。只要确保您在 urls.py
中正确指定了URL即可。解决这些问题,然后看看是否能解决问题。
If you use Django's DeleteView, you don't need to specify a url in the template, it will do all the work for you. Just make sure you specify the url correctly in your urls.py
. Fix these issues and see if it works then.
这篇关于如何删除答案(正文中的代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!