用Django上传jquery ajax文件

时间:2022-12-09 19:39:03

I'm using jquery ajaxfileupload plugin http://www.phpletter.com/Our-Projects/AjaxFileUpload/ to upload images with Django in server-side. I've also done the ajax setup which I saw here Django CSRF check failing with an Ajax POST request But still I'm getting 403 Forbidden: csrf verification failed.

我正在使用jquery ajaxfileupload插件http://www.phpletter.com/Our-Projects/AjaxFileUpload/在服务器端使用Django上传图像。我也做了ajax设置,我在这里看到Django CSRF检查失败了Ajax POST请求但是我仍然得到403 Forbidden:csrf验证失败。

Here is the client side script: http://jsfiddle.net/rkumarnirmal/FSDPH/

这是客户端脚本:http://jsfiddle.net/rkumarnirmal/FSDPH/

Here is the Django code:

这是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("")  

Could anyone help me?

谁能帮助我?

Thanks!

2 个解决方案

#1


1  

Make sure to use RequestContext in your views.

确保在视图中使用RequestContext。

See https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it

#2


0  

Attach the annotation @csrf_protect at the top of the view method!

在视图方法的顶部附加注释@csrf_protect!

#1


1  

Make sure to use RequestContext in your views.

确保在视图中使用RequestContext。

See https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it

#2


0  

Attach the annotation @csrf_protect at the top of the view method!

在视图方法的顶部附加注释@csrf_protect!