Answer the question
In order to leave comments, you need to log in
How to correctly implement post requests for DetailView?
How to correctly implement post requests for DetailView? what I'm doing is just throwing a 405 error
class NewsDetailView(DetailView):
model = NewsModel
def get_context_data(self, **kwargs):
context = super().get_context_data()
comment_form = CommentForm()
context['comment_form'] = comment_form
return context
def post(self, request, pk):
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment_form['news_item'] = pk
comment_form.save()
# CommentModel.objects.create(** comment_form.cleaned_data)
return HttpResponseRedirect('/news')
return render(request, 'app_news/newsmodel_detail.html', context={'comment_form': comment_form})
Answer the question
In order to leave comments, you need to log in
That's right - do not override the post method in the DetailView heir and do not manually validate the form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question