我应该在这里使用HttpResponseRedirect吗? [英] Should I use HttpResponseRedirect here?

查看:67
本文介绍了我应该在这里使用HttpResponseRedirect吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django进行Tango教程,并且已经成功完成了教程,但是我在

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

这里要注意的部分是在成功处理POST数据后始终返回HttpResponseRedirect".但是,在Tango with Django教程中:

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)

请注意,尽管使用了POST数据,但缺少HttpResponseRedirect.我不知道这是否正确?

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

我在这里查看: Django HttpResponseRedirect

此处: Django:HttpResponseRedirect不起作用

在这里:此外,在这里:使用HttpResponseRedirect进行Django表单重定向

最后是这里: Django:什么HttpResponse与HttpResponseRedirect与render_to_response的区别是什么

我仍然不完全了解如何使用HttpResponseRedirect.请帮助.

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

首先感谢任何回应.

推荐答案

这是防止用户在服务器端处理初始POST请求后重新提交表单的常见做法.

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

如果在处理POST请求后不使用HttpResponseRedirect,后果可能是您无意中将多个重复的行插入数据库或多次发送确认电子邮件等.

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.

这篇关于我应该在这里使用HttpResponseRedirect吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆