csrf token fetch django 1

csrf token fetch django

let data = {
    'file': file,
    'fileName': file.name,
// You have to download 3rd Cookies library
// https://docs.djangoproject.com/en/dev/ref/csrf/#ajax
let csrftoken = Cookies.get('csrftoken');
let response = fetch("/upload/", {
    method: 'POST',
    body: JSON.stringify(data),
    headers: { "X-CSRFToken": csrftoken },

Here is what the above code is Doing:
1. Create a FormData object and append the file to it.
2. Create a data object and add the file and fileName to it.
3. Get the csrftoken from the cookie.
4. Send the data to the server using fetch.

Similar Posts