1

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.

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

Here is the Django code:

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!

Community
  • 1
  • 1
rnk
  • 2,174
  • 4
  • 35
  • 57

2 Answers2

1

Make sure to use RequestContext in your views.

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

cfedermann
  • 3,286
  • 19
  • 24
  • I'm using ajax method so I've included the code I've seen here https://www.djangoproject.com/weblog/2011/feb/08/security/ – rnk Apr 13 '12 at 11:29
0

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

Stephan
  • 41,764
  • 65
  • 238
  • 329