csrf_exempt 装饰器不豁免

csrf_exempt decorator not exempting

我有一个 wordpress 网站(用 PHP 编码),我用它来 post 数据到 django 视图。我 post 使用此代码

$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://mydjangosite.com/blah/blah2/');

上面 url 的功能是使用 @csrf_exempt 装饰器,因为我想在这种情况下允许跨站点 post

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
@api_wrapper
def add_referral_api(request, status_slug):

但是,我的日志中仍然收到此错误

[03/Feb/2017 18:17:48] WARNING [django.request:177] Forbidden (CSRF cookie not set.): 

如何允许在受信任站点之间进行此类跨站点 posting?

编辑 我的中间件 类 就是这样。请注意,该站点使用 ssl 证书,这是值得的,我怀疑额外的安全性导致 csrf_exemption 装饰器无法像我希望的那样工作。即便如此,我还是想以某种方式能够说这个站点(我的 WP 站点)可以从中接收 post 数据。

MIDDLEWARE_CLASSES = (
    # This middleware is for ensuring that all pages use https
    #'djangosecure.middleware.SecurityMiddleware',
    'django.middleware.gzip.GZipMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'common.middleware.XsSharing',
    'impersonate.middleware.ImpersonateMiddleware',
    'referrals.middleware.ReferralMiddleware',
)

在与 Matt 交谈后,我们发现罪魁祸首是 @api_wrapper 装饰器(因此,我最初的直觉是正确的),因为它调用了 @ensure_csrf_token 装饰器,渲染了 @csrf_exempt没用。 PHP 调用给出了 Bad Request (400),但这超出了这个问题的范围。