2016-04-15 36 views
2

私のDjango WebサイトはHTTPSになっています。スクリプトからウェブサイトにデータをPOSTしようとしているとき、私はこのエラーを受け取ります: "referer checking failed - no Referer"それはCSRFの問題だと思われますが、私はそれを解決する方法がわかりません。POSTリクエストでDjango CSRFの検証に失敗しました:リファラーチェックに失敗しました - いいえReferer

例:

[...] 

<p>Reason given for failure:</p> 
<pre> 
Referer checking failed - no Referer. 
</pre> 

<p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when 
<a 
href="https://docs.djangoproject.com/en/1.8/ref/csrf/">Django's 
CSRF mechanism</a> has not been used correctly. For POST forms, you need to 
ensure:</p> 

<ul> 
<li>Your browser is accepting cookies.</li> 

<li>The view function passes a <code>request</code> to the template's <a 
href="https://docs.djangoproject.com/en/dev/topics/templates/#django.template.backends.base.Template.render"><code>render</code></a> 
method.</li> 

<li>In the template, there is a <code>{% csrf_token 
%}</code> template tag inside each POST form that 
targets an internal URL.</li> 

<li>If you are not using <code>CsrfViewMiddleware</code>, then you must use 
<code>csrf_protect</code> on any views that use the <code>csrf_token</code> 
template tag, as well as those that accept the POST data.</li> 

</ul> 

[...] 

私はPOSTデータを送信する前に私のヘッダにリファラを渡す必要があります - 便利ではないでしょう。

import requests 
r = requests.post('https://mywebsite/mypage', data = {'key':'value'}) 
print r.text 

が私にこの出力を与えますか?または、このページでCSRFを無効にする必要がありますか?

おかげ

+0

私はこの問題にも問題があります。すべての回答Julien Salinas? – neelima

+0

@neelimaビューでCSRFチェックを無効にする必要があります。 '@ csrf_exempt'デコレータを使用してください:https://docs.djangoproject.com/en/1.11/ref/csrf/#django.views.decorators.csrf.csrf_exempt –

答えて

2

は私の知る限り、これは未知の奇妙なソースからのデータを掲載避けるために、CSRFの目的です。 djangoが動的に生成するcsrfトークンが必要です。