【问题标题】:Why is redirection important after dealing with POST data?为什么在处理 POST 数据后重定向很重要?
【发布时间】:2014-01-31 17:52:38
【问题描述】:

此代码来自Django documentation on forms

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from polls.models import Choice, Poll

def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
try:
    selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
    # Redisplay the poll voting form.
    return render(request, 'polls/detail.html', {
        'poll': 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,)))

我正在学习 Django 框架,但我不明白为什么在处理 POST 数据后重定向对安全性很重要。

其实下面有一个解释:

成功处理后总是返回一个 HttpResponseRedirect 使用 POST 数据。这可以防止数据被发布两次,如果 用户点击返回按钮。

有人可以进一步解释一下吗?

【问题讨论】:

  • 这不是为了安全 - 在成功发布后发送重定向的模式中没有任何东西可以防止恶意用户。这是为了方便您的用户,特别是防止某些意外的双重提交或在非预期状态下提交,如flasetru的链接中所述。我不反对这个参考——我只是想强调一下,为了安全起见,你必须记住,任何类型的请求都可以随时与任何数据一起提交,并且你的服务器端应用程序必须适当地处理潜在的恶意数据.

标签: django redirect


【解决方案1】:

表示用户可以通过点击F5(Back-Sumbit)(Back-Sumbit)(Back-Sumbit)多次发帖

如果不是返回重定向?

我认为它可以帮助防止灌溉。

【讨论】:

  • 我认为,它是一个评论。
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多