Django CSRF验证失败。即使使用@csrf_exempt,请求也会中止

时间:2016-12-12 12:38:38

标签: python django django-1.9 ccavenue

这是我的view.py我试图从支付网关获得回复 但是获得 403 Forbidden 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 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)

我在stackoverflow上阅读了很多解决方案并试过但仍然无法正确使用

我的主要urls.py

url(r'^booked/', include('booking.urls')),

我在app中的urls.py名为预订

urlpatterns = patterns('',
url(r'^responce-cc/', "booking.views.resp", name="cc_response_url"),)

我传递给支付网关的重定向网址是

https://www.mysitename.com/booked/responce-cc/

2 个答案:

答案 0 :(得分:0)

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)

使用上面的代码将所有内容都放在适当的位置! :)

答案 1 :(得分:-1)

在身体之间的某处添加{% csrf_token %}