我正在使用 jquery ajaxfileupload 插件 http://www.phpletter.com/Our-Projects/AjaxFileUpload/ 在服务器端使用 Django 上传图像。我还完成了我在这里看到的ajax设置Django CSRF check failing with an Ajax POST request 但我仍然收到 403 Forbidden:csrf 验证失败。
这是客户端脚本: http://jsfiddle.net/rkumarnirmal/FSDPH/
这是 Django 代码:
def backgroundview(request):
if request.is_ajax():
b = request.POST.get('fileToUpload')
try:
g = BackgroundModel.objects.get(user=request.user)
except CoverModel.DoesNotExist:
bm = BackgroundModel(background=b)
bm.user = request.user
bm.save()
else:
g.background = b
g.save()
return HttpResponse("")
有人可以帮助我吗?
谢谢!
最佳答案
确保在您的 View 中使用RequestContext
。
参见https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it
关于使用 Django jquery ajax 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10139770/