After Django submits the form to refresh, the problem of repeated submission

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688707&siteId=291194637