Django 中的 CSRF 验证失败
CSRF verification failed in django
我正在尝试完成 geo Django tutorial,它已经很旧了 (1.3)。到目前为止,我做得很好,但我遇到了一个特定的错误。
我正在尝试构建将一些数据保存到数据库中的 table 的功能。
这是我的观点:
# 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')
当我 select 保存按钮时出现错误(在 firebug 中):403 Forbidden
现在我知道这与以下内容有关:
<h1>Forbidden <span>(403)</span></h1>
<p>CSRF verification failed. Request aborted.</p>
但我不知道如何解决它。
正如@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_token %} 添加到您的 Django 模板中。你需要一个表单标签才能工作,无论如何你都应该有一个。否则,浏览器如何知道将数据发送到哪里?
<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
我正在尝试完成 geo Django tutorial,它已经很旧了 (1.3)。到目前为止,我做得很好,但我遇到了一个特定的错误。
我正在尝试构建将一些数据保存到数据库中的 table 的功能。 这是我的观点:
# 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')
当我 select 保存按钮时出现错误(在 firebug 中):403 Forbidden 现在我知道这与以下内容有关:
<h1>Forbidden <span>(403)</span></h1>
<p>CSRF verification failed. Request aborted.</p>
但我不知道如何解决它。
正如@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_token %} 添加到您的 Django 模板中。你需要一个表单标签才能工作,无论如何你都应该有一个。否则,浏览器如何知道将数据发送到哪里?
<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