【问题标题】:CSRF verification failed in djangodjango中的CSRF验证失败
【发布时间】:2016-02-15 17:19:11
【问题描述】:

我正在尝试完成一个相当旧的geo Django tutorial (1.3)。直到现在我做得很好,但我遇到了一个特定的错误。

我正在尝试构建将一些数据保存到 db 表中的功能。 这是我的看法:

# Import django modules
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.http import HttpResponse
import simplejson
from waypoints.models import Waypoint

def save(request):
'Save waypoints'
for waypointString in request.POST.get('waypointsPayload', '').splitlines():
    waypointID, waypointX, waypointY = waypointString.split()
    waypoint = Waypoint.objects.get(id=int(waypointID))
    waypoint.geometry.set_x(float(waypointX))
    waypoint.geometry.set_y(float(waypointY))
    waypoint.save()
return HttpResponse(simplejson.dumps(dict(isOk=1)),    mimetype='application/json')

当我选择保存按钮时,我收到一个错误(在 firebug 中):403 Forbidden 现在我知道这与:

<h1>Forbidden <span>(403)</span></h1>
<p>CSRF verification failed. Request aborted.</p>

但我不知道如何解决它。

【问题讨论】:

  • 要么在 HTML &lt;form&gt; 标记之后添加 {% csrf_token %}(安全),要么在 def save(request): 之前添加 @csrf_exempt(不安全)
  • @Selcuk 非常感谢您的回答。你能说得更具体一点吗?现在我没有任何表单标签。只是: 我应该将它添加到输入中吗?
  • 如何在没有表单标签的情况下发布?通过Javascript?
  • 是的。使用 jQuery。它只是一个我尝试完成的教程,所以我对 geodjango 有了第一个想法!

标签: python django geodjango


【解决方案1】:

正如@Selcuk 建议的那样,在视图函数上使用Django 装饰器csrf_exempt 应该可以解决此问题。但是,请注意它不会保护您的请求免受 CSRF 攻击。
你可以阅读更多关于它是如何工作的here

# Import django modules
from django.http import HttpResponse
# import csrf_exempt
from django.views.decorators.csrf import csrf_exempt
# Import system modules
import simplejson
# Import custom modules
from googlemaps.waypoints.models import Waypoint

@csrf_exempt 
def save(request):
    'Save waypoints'
    for waypointString in request.POST.get('waypointsPayload', '').splitlines():
        waypointID, waypointX, waypointY = waypointString.split()
        waypoint = Waypoint.objects.get(id=int(waypointID))
        waypoint.geometry.set_x(float(waypointX))
        waypoint.geometry.set_y(float(waypointY))
        waypoint.save()
    return HttpResponse(simplejson.dumps(dict(isOk=1)), mimetype='application/json')

【讨论】:

  • 除非你有充分的理由,否则不要使用它。 CSRF 是一种安全措施,用于避免其他人代表您发布数据。有关安全解决方案,请参阅我的答案。
【解决方案2】:

解决这个问题的正确方法是在你的 Django 模板中添加一个 {% csrf_token %}。你需要一个表单标签才能工作,无论如何你都应该有一个。否则,浏览器如何知道将您的数据发送到哪里?

<form action="" method="post">
    {% csrf_token %}
    <input id=saveWaypoints type=button value=Save disabled=disabled>
</form

Django 文档提供了很多关于 CSRF 工作原理及其重要性的信息: https://docs.djangoproject.com/en/1.9/ref/csrf/#how-to-use-it

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 2015-06-02
    • 1970-01-01
    • 2017-03-29
    • 2011-06-14
    • 2020-03-24
    相关资源
    最近更新 更多