3

I want to retrieve json data and put it to database. I'm using django

When i post json i receive this error: Forbidden (403) CSRF verification failed. Request aborted.

Code

def receiver(request):
    try:
        json_data = request_to_json(request)  

        # Retrieving json data
        x = json_data['x']
        y = json_data['y']
        z = json_data['z']
        # put it in database

        db = db_connection().db;
        db_manager= db_management(db);
        db_manager.insert_point(x,y,z,x);
        db.close() 

        # A variable to return to the app
        response = 'OK'

    except:
        response = 'Error'

    return HttpResponse(simplejson.dumps(response))

2 Answers 2

2

This is the solution i found:

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')
Sign up to request clarification or add additional context in comments.

Comments

1

This error is raised, when you try to post data outer than a django form.

The simple, but UNSAFE method is to add @csrf_exempt before your views, like:

@csrf_exempt
def my_view(request):
    pass

The not that simple method is written here: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

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.