【问题标题】:Django CSRF verification failed. Request aborted even with @csrf_exemptDjango CSRF 验证失败。即使使用@csrf_exempt,请求也会中止
【发布时间】:2016-12-12 12:38:37
【问题描述】:

这是我的 view.py 我正在尝试从支付网关获得响应 但是 m 得到 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')),

我在名为 booking

的应用中的 urls.py
urlpatterns = patterns('',
url(r'^responce-cc/', "booking.views.resp", name="cc_response_url"),)

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

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

【问题讨论】:

  • 使用@csrf_exempt,我认为您不会收到 CSRF 验证错误。你一定是在调用另一个视图。
  • 不,我有正确的视图 url,但仍然得到相同的错误
  • 仍然存在错误。也许发布您的整个 urlconf 和您正在调用的 URL?
  • 我已为以下问题添加了 urls.py
  • 我遇到了同样的问题,在另一个项目中再次出现,奇怪的是我只是在其他应用程序的视图(同一个项目)中粘贴了带有 csrf_exempt 的视图并配置了网址,它工作了!!!跨度>

标签: python django django-1.9 ccavenue


【解决方案1】:
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)

使用上面的代码让一切就绪! :)

【讨论】:

  • 这和我写的代码一样,我也导入了模板、HttpResponse和加密、解密。
【解决方案2】:

在正文之间的某处添加{% csrf_token %}

【讨论】:

  • 我无法添加令牌,响应是从支付网关生成的
猜你喜欢
  • 2016-09-09
  • 2012-12-07
  • 2015-03-08
  • 2017-06-26
  • 2014-01-20
  • 1970-01-01
  • 2014-02-03
  • 2016-05-29
相关资源
最近更新 更多