Made a comment function with Django
//前端 html
<form class="form" action="{{ resume.get_comment_url }}" method="post">
.....
</form>
//views.py
.....
entry = Resume.objects.get(id=pk)
form = CommentForm(data=request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.entry = entry
new_comment.author = request.user
new_comment.save()
return render(request, 'recommendation/detail.html', context={'resume': entry, })
.....
If the page refreshes in views render, you can see the new comment.
But then F5 refresh, will prompt the form to submit repeatedly
Use HttpResponseRedirect instead to jump
Under Resume model
def get_absolute_url(self):
return reverse('recommendation:resume', kwargs={'pk': self.pk})
changes in views.py
return HttpResponseRedirect(entry.get_absolute_url())