Dje*_*ent 37 python django http-post http-status-code-403
当我使用以下Python代码向我的Django网站发送POST请求时,我得到403:Forbidden错误.
url = 'http://www.sub.domain.com/'
values = { 'var': 'test' }
try:
data = urllib.urlencode(values, doseq=True)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
except:
the_page = sys.exc_info()
raise
Run Code Online (Sandbox Code Playgroud)
当我打开任何其他网站时,它正常工作.domain.com也是Django网站,也可以正常使用.我认为,这是Django配置问题,任何人都可以告诉我应该怎么做才能提供对我脚本的访问权限?
bx2*_*bx2 47
请看这里https://docs.djangoproject.com/en/dev/ref/csrf/#how-to-use-it.
尝试使用标记您的视图@csrf_exempt.这样,Django的CSRF中间件将忽略CSRF保护.你还需要使用from django.views.decorators.csrf import csrf_exempt.请参阅:https://docs.djangoproject.com/en/dev/ref/csrf/#utilities
请注意,通过在您的视图中禁用CSRF保护,您正在打开CSRF攻击的大门.
如果安全性对您至关重要,那么请考虑使用@csrf_exempt后跟@requires_csrf_token(参见:https://docs.djangoproject.com/en/dev/ref/csrf/#unprotected-view-needs-the-csrf-token).然后,在您的脚本中传递此令牌,就是这样.
Joe*_*e J 29
您要发布的视图是否包含Django表单?如果是这样,我想知道它是否给出了csrf错误.我认为这表现为403.在这种情况下,您需要添加{{csrf_token}}标记.只是一个想法.