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