gpt4 book ai didi

django - 为什么处理 POST 数据后重定向很重要?

转载 作者:行者123 更新时间:2023-12-02 14:53:55 25 4
gpt4 key购买 nike

此代码来自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 数据后重定向对于安全性很重要。

其实下面有相关的解释:

Always return an HttpResponseRedirect after successfully dealing with POST data. This prevents data from being posted twice if a user hits the Back button.

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

最佳答案

这意味着用户可以通过点击F5(Back-Sumbit)(Back-Sumbit)(Back-Sumbit)多次发布

如果不是返回重定向呢?

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

关于django - 为什么处理 POST 数据后重定向很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21083598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com