Should I use HttpResponseRedirect here?

I am doing a tango lesson with Django and I have successfully completed the lessons, however, I noticed the following in the official Django Surveys lesson :

def vote(request, question_id): p = get_object_or_404(Question, pk=question_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the question voting form. return render(request, 'polls/detail.html', { 'question': p, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) 

Note: "Always return an HttpResponseRedirect after successfully processing POST data." However, in the Tango lesson with Django:

 def add_page(request, category_name_url): context = RequestContext(request) category_name = decode_url(category_name_url) if request.method == 'POST': form = PageForm(request.POST) if form.is_valid(): page = form.save(commit=False) try: cat = Category.objects.get(name=category_name) page.category = cat except Category.DoesNotExist: return render_to_response('rango/add_category.html', {}, context) page.views = 0 page.save() return category(request, category_name_url) else: print(form.errors) else: form = PageForm() return render_to_response('rango/add_page.html', {'category_name_url': category_name_url, 'category_name' : category_name, 'form' : form}, context) 

Note the lack of an HttpResponseRedirect, despite the use of POST data. I do not know if this is correct or not?

I looked here: Django HttpResponseRedirect

Here: Django: HttpResponseRedirect not working

And here: Django HttpResponseRedirect vs render_to_response - how to get the login form to behave the way I need

Also here: redirecting a Django form using HttpResponseRedirect

And finally, here: Django: What is the difference between b / w HttpResponse and HttpResponseRedirect vs render_to_response

I still do not fully understand how to use HttpResponseRedirect. Please help.

Thanks in advance to everyone who responds.

+8
python django
source share
2 answers

It is common practice to prevent the user from re-submitting the form after the initial POST request has been processed on the server side.

If you do not use HttpResponseRedirect after processing the POST request, the consequences may be that you accidentally insert multiple duplicate rows into your database or send an email with confirmation more than once, etc.

+2
source

Note the lack of an HttpResponseRedirect, despite the use of POST data. I don’t know if it is right or not?

Both are "correct" and will work fine. However, preventing re-sending via redirection is the best approach in terms of user interface design if the user clicks the return or update buttons in their browser.

0
source

All Articles