I have a question about django+axios csrf 403.

Asked 2 years ago, Updated 2 years ago, 146 views

Currently, the front is configured using View and Axios, and the server side is under project with django.

csrf 403 error while sending video file to server.

What I did to prevent csrf error now is

The JavaScript code is set to axios csrf_token.


axios.defaults.xsrfCookieName = 'csrftoken';
axios.defaults.xsrfHeaderName = 'x-CSRFToken';
axios.defaults.withCredentials = true

post('/convert/upload/', form)
           .then(function (res) {
            if (vm.fileList.length === 1) {
                            vm.fileNamesWithCheck[0].convertedFileUrl = res.data.url_one;
                            vm.fileNamesWithCheck[0].fileUploaded = true                        
                        } } else {
                            vm.fileNamesWithCheck[0].convertedFileUrl = res.data.url_one;                       
                        }
                        // Checkconverted button true to make the convert button appear
                        vm.forConvertButton[0].checkConverted = true;
                        // Remove portion of file size after sending
                        vm.fileSizeCheck = false;
                    })

In the django setting.py file, CSRF_COOKIE_NAME = "XSRF-TOKEN",

@sense_csrf_cookie over the view.py function corresponding to post I've set it up.

The csrf 403 error occurs even though these settings are set. Is there anything I missed in the Axios setting?

axios django vue

2022-09-20 22:30

1 Answers

If you're using the janggu, Position @sense_csrf_cookie at the top of the view (just below import)

This will eliminate the csrf problem!


2022-09-20 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.