How can I delete the answers (code in body)?

寵の児 提交于 2020-07-23 06:45:18

问题


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(not 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/$']

Thanks in advance.

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:


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).

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:

https://docs.djangoproject.com/en/3.0/ref/class-based-views/generic-editing/#django.views.generic.edit.DeleteView

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.




回答2:


May Be This Might Work For You, Because You Never Used The Import Of 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')

Happy Coding, let me know if there are any issues with this I'am Up for you!



来源:https://stackoverflow.com/questions/62964507/how-can-i-delete-the-answers-code-in-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!