Search code examples
djangotwitter-bootstrapbootstrap-file-upload

krajee bootstrap-fileinput with django framework


I am trying to use this, krajee bootstrap-fileinput library with Django python framework. I need to pass csrf_token via headers to submit the file via ajax to the server. Where should I put csrf_token? In general, I used to pass as a header in ajax method in jquery. Now I don't understand where should I have to include csrf_token. Do I have to put in fileinput?


Solution

  • Although this is a very old question, here is the answer. I had the same issue with using the bootstrap-file-upload plugin and I solved it by adding the crsf token to as uploadExtraData

    Laravel adds the token into a globally available variable

    <script>
        window.Laravel = {"csrfToken":"PQwQZyeHaARm0gf52qzvIdl3T203FhmPT90K2bLX"};
    </script>
    

    You can then make use of it, when assigning the bootstrap-file-upload widget

    $(document).ready(function(){
      $("#file_uplooad").fileinput({
           uploadUrl:'/ajax/fileupload', 
           uploadExtraData:{
             _token:window.Laravel.csrfToken // we pass it here to the server
           }
        });
     });
    

    Although this is a very late answer, I still hope that someone will benefit from it. Have fun!