Python Django CSRF验证失败。即使@csrf_豁免,请求也已中止
这是我的视图。我正在尝试从支付网关获得响应 但是我得到一个403禁止CSRF验证失败。请求中止。付款后,我豁免了视图的CSRF令牌,但仍然显示相同的错误Python Django CSRF验证失败。即使@csrf_豁免,请求也已中止,python,django,django-1.9,ccavenue,Python,Django,Django 1.9,Ccavenue,这是我的视图。我正在尝试从支付网关获得响应 但是我得到一个403禁止CSRF验证失败。请求中止。付款后,我豁免了视图的CSRF令牌,但仍然显示相同的错误 from django.views.decorators.csrf import csrf_exempt @csrf_exempt def resp(request, encResp): print " RESPONSE WITH CSRF EXEMPT " ''' Please put in the 32 bit
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def resp(request, encResp):
print " RESPONSE WITH CSRF EXEMPT "
'''
Please put in the 32 bit alphanumeric key in quotes provided by CCAvenues.
'''
workingKey = WorkingKey
decResp = decrypt(encResp,workingKey)
data = '<table border=1 cellspacing=2 cellpadding=2><tr><td>'
data = data + decResp.replace('=','</td><td>')
data = data.replace('&','</td></tr><tr><td>')
data = data + '</td></tr></table>'
html = '''\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Response Handler</title>
</head>
<body>
<center>
<font size="4" color="blue"><b>Response Page</b></font>
<br>
$response
</center>
<br>
</body>
</html>
'''
fin = Template(html).safe_substitute(response=data)
return HttpResponse(fin)
名为“预订”的应用程序中的My urls.py
urlpatterns = patterns('',
url(r'^responce-cc/', "booking.views.resp", name="cc_response_url"),)
我传递给支付网关的重定向url是
https://www.mysitename.com/booked/responce-cc/
在正文之间添加
{%csrf\u token%}
。从ccavutil导入加密、解密
from ccavutil import encrypt,decrypt
from string import Template
from django.http import HttpResponse
def res(encResp):
'''
Please put in the 32 bit alphanumeric key in quotes provided by CCAvenues.
'''
workingKey = 'WorkingKey'
decResp = decrypt(encResp,workingKey)
data = '<table border=1 cellspacing=2 cellpadding=2><tr><td>'
data = data + decResp.replace('=','</td><td>')
data = data.replace('&','</td></tr><tr><td>')
data = data + '</td></tr></table>'
html = '''\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Response Handler</title>
</head>
<body>
<center>
<font size="4" color="blue"><b>Response Page</b></font>
<br>
$response
</center>
<br>
</body>
</html>
'''
fin = Template(html).safe_substitute(response=data)
return HttpResponse(fin)
从字符串导入模板
从django.http导入HttpResponse
def res(ENCREP):
'''
请在CCA提供的引号中输入32位字母数字键。
'''
workingKey='workingKey'
decResp=解密(encResp,工作密钥)
数据=“”
数据=数据+递减替换('=','')
数据=数据。替换('&','')
数据=数据+“”
html=“”\
响应处理程序
响应页面
$response
'''
fin=模板(html).安全替换(响应=数据)
返回HttpResponse(fin)
使用上述代码将所有内容都准备就绪!:) 有了@csrf_豁免,我认为你不会得到一个csrf验证错误。你一定是在调用另一个视图。不,我有正确的视图url,但仍然得到相同的错误。错误仍然在其中的某个地方。也许发布你的整个urlconf和你正在调用的URL?我已经为下面的问题添加了urls.py。我也有同样的问题,在另一个项目上,我只是在其他应用程序的视图(同一个项目)中粘贴了带有
csrf_export
的视图,并配置了URL,结果成功了!!!我无法添加令牌,响应是从支付网关生成的。它与我编写的代码相同,而且我导入了模板、HttpResponse和encrypt、decrypt。
from ccavutil import encrypt,decrypt
from string import Template
from django.http import HttpResponse
def res(encResp):
'''
Please put in the 32 bit alphanumeric key in quotes provided by CCAvenues.
'''
workingKey = 'WorkingKey'
decResp = decrypt(encResp,workingKey)
data = '<table border=1 cellspacing=2 cellpadding=2><tr><td>'
data = data + decResp.replace('=','</td><td>')
data = data.replace('&','</td></tr><tr><td>')
data = data + '</td></tr></table>'
html = '''\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Response Handler</title>
</head>
<body>
<center>
<font size="4" color="blue"><b>Response Page</b></font>
<br>
$response
</center>
<br>
</body>
</html>
'''
fin = Template(html).safe_substitute(response=data)
return HttpResponse(fin)