Question

I am doing the Tango with Django tutorial and I have completed the tutorials successfully however I noticed in the official Django Polls tutorial, the following:

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

The part to notice here is the "Always return an HttpResponseRedirect after successfully dealing with POST data." However, in the Tango with Django tutorial:

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)

Notice the lack of HttpResponseRedirect despite the use of POST data. I don't know if this is correct or not?

I have looked here: Django HttpResponseRedirect

Here: Django: HttpResponseRedirect not working

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

Also, here: Django form redirect using HttpResponseRedirect

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

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

Thanks in advance to anyone who responds.

Était-ce utile?

La solution

This is common practice to prevent the user resubmitting the form after the initial POST request has been processed server side.

If you don't use HttpResponseRedirect after processing a POST request, the consequences might be that you inadvertently insert multiple duplicate rows into your database or send a confirmation email more than once etc.

Autres conseils

Notice the lack of HttpResponseRedirect despite the use of POST data. I don't know if this is correct or not?

Both are "correct" and will work fine. However, preventing re-submission with a redirect is a better approach from a UI design perspective, in case the user hits the back or refresh buttons on their browser.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top