Firefox doesn't get django csrf_token

I submit an ajax form in django and using

xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); 

to get csrf_token. The mold works well in chrome. But in firefox, the csrf_token value is null and gives a 403 forbidden error. I do not get csrf_token in the console when I checked cookies in the console. Why django does not give csrf_token to firefox browser?

+4
source share
1 answer

Add the following kind of decorator to the view that creates the page containing the form

@ensure_csrf_cookie

From Django Docs -

Page uses AJAX without any HTML form

The page makes a POST request through AJAX, and the page does not have an HTML form with csrf_token, which will call the required CSRF cookie to send.

Solution: use view_csrf_cookie () in the view that sends the page.

+4
source

All Articles