Python CSRF验证失败。请求被中止,csrf_豁免
我试图在我的视图中从另一个站点接收post请求,但在我的视图中收到此错误:Python CSRF验证失败。请求被中止,csrf_豁免,python,django,Python,Django,我试图在我的视图中从另一个站点接收post请求,但在我的视图中收到此错误: Forbidden <span>(403) CSRF verification failed. Request aborted. Reason given for failure: CSRF cookie not set. 我已经检查了文档并添加了csrf_豁免,需要csrf_令牌这很奇怪。。。不知道会发生什么。请注意,添加@requires\u csrf\u token不应该有帮助——这
Forbidden <span>(403)
CSRF verification failed. Request aborted.
Reason given for failure:
CSRF cookie not set.
我已经检查了文档并添加了csrf_豁免,需要csrf_令牌这很奇怪。。。不知道会发生什么。请注意,添加
@requires\u csrf\u token
不应该有帮助——这是用于发送csrf token验证请求的页面,而不是用于接收非csrf验证请求的页面(尽管包含它不会产生任何后果)。表明这个问题可能是由于使用Django REST框架引起的,它的SessionBackend
强制使用CSRF令牌,即使您的视图说它们不是必需的。您是否也在使用Django REST Framework?@JeremyBanks我没有使用您添加到模板表单中的REST FrameworkId{%csrf\u token%}
{%csrf\u token%}
就像他们在2点中说的那样。
@Liarez我的模板中没有使用任何形式。我只是作为POST请求从另一方接收数据。
@csrf_exempt
@requires_csrf_token
def notificacao(request):
if request.method == 'POST':
notification_code = request.POST['notificationCode']
if notification_code:
url = 'https://ws.pagseguro.uol.com.br/v2/transactions/notifications/' + notification_code + '?email=filipe.ferminiano@gmail.com' + '&token=token'
r = requests.get(url)
print r['status']
if r['status']:
b = teste(name = r['status'])
else:
b = teste(name = 'teste errado')
b.save()
print 'r ' + r
return render(request, 'obrigado.html',{'code':notification_code})
else:
print 'notification code is null'
b = teste(name = 'sem notification code')
b.save()
return render(request,'obrigado.html')
else:
b = teste(name = 'sem metodo post')
b.save()
return render(request, 'obrigado.html')