I am creating a question-and-answer app, I have some views that I want to combine into a single view. I want to combine the QuestionDetailView, DeleteQuestionView and AnswerFormView into AnswerView.
The problem is that anytime I click on submit to submit an answer to a question, it deletes the question instead.
I have read that you can’t have two post requests in a single view, please I would appreciate it if anyone could give me a help on this.
Here is my views and detail_view.html template.
views.py
class QuestionDetailView(DetailView):
model = Question
context_object_name = "question"
template_name = "qa/question_detail.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"] = AnswerForm()
return context
class QuestionDeleteView(PermissionRequiredMixin, DeleteView):
permission_required = "qa.can_answer"
model = Question
template_name = "qa/question_detail.html"
success_url = reverse_lazy("unanswered_questions")
class AnswerFormView(PermissionRequiredMixin, SingleObjectMixin, FormView):
permission_required = "qa.can_answer"
model = Question
form_class = AnswerForm
template_name = "qa/question_detail.html"
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
answer = form.save(commit=False)
answer.question = self.object
answer.save()
return super().form_valid(form)
def get_success_url(self):
return reverse(
"question_detail",
kwargs={"question_id": self.object.question_id, "slug": self.object.slug},
)
class AnswerView(View):
template_name = "qa/question_detail.html"
def get(self, request, *args, **kwargs):
view = QuestionDetailView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = AnswerFormView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = QuestionDeleteView.as_view()
return view(request, *args, **kwargs)
detail_view.html
{% extends "qa/base_generic.html" %} {% block content %}
<div class="container">
<h1 class="display-4">{{ question.title }}</h1>
<p class="lead">{{ question.content }}</p>
{% if question.answer %}
<h2 class="mt-4">Answer</h2>
<div class="card my-2">
<div class="card-body">
<p class="card-text">{{ answer.content }}</p>
<p class="card-text">
<small class="text-muted"
>Posted on: {{ answer.created_at|date:"F d, Y H:i" }}</small
>
</p>
<p class="card-text">
<small class="text-muted"
>Last Updated: {{ answer.updated_at|date:"F d, Y H:i" }}</small
>
</p>
</div>
</div>
{% else %}
<p class="mt-4">No Answer Available</p>
{% if perms.qa.can_answer %}
<form method="post" class="my-4">
{% csrf_token %} {{ form.as_p }}
<button class="btn btn-primary" type="submit">Submit Answer</button>
</form>
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Delete</button>
</form>
{% endif %} {% endif %}
</div>
{% endblock %}