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!

3
  • Are you adding the {% csrf_token %} to the rendered form? Commented Apr 13, 2012 at 11:19
  • I've tried adding the tag but still getting the same 403 forbidden error Commented Apr 13, 2012 at 11:22
  • Can you use alert(getCookie('csrftoken')) to check if you have it set? Commented Apr 13, 2012 at 11:23

2 Answers 2

1

Make sure to use RequestContext in your views.

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using ajax method so I've included the code I've seen here djangoproject.com/weblog/2011/feb/08/security
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.