how to pass csrf_token to javascript file in django? -


This is my JavaScript code that works fine but I like to keep javascript files separate and use as inline script tags Do not

  & lt; Script & gt; '$ {' Book '} Click (function () (var {id} Url: '/ post / book /', cache: 'wrong', data type: 'jason', type: 'post', data: data, success: work (data) {/ else {// something}}, Error: function (error) {warning ('error;' + evil (error));}}); return returned;}); }); & Lt; / Script & gt;  

I want to include it in my custom.js file which I have included in my base.html. Which is loading steady% from

  {staticfiles%} {% load bootstrap3%} & lt ;! DOCTYPE html & gt; & Lt; Html lang = "en" & gt; & Lt; Top & gt; & Lt; Meta charset = "UTF-8" & gt; & Lt; Meta name = "viewport" content = "width = device-width, initial-scale = 1.0" & gt; & Lt; Title & gt; {% Block title%} {% endblock%} & lt; / Title & gt; {% Bootstrap_css%} & lt ;! - Optional theme - & gt; & Lt; Link rel = "stylesheet" href = "// netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> & Lt; Link href = "{% static" css / custom.css "%}" rel = "stylesheet" & gt; & Lt ;! - Latest compiled and minified javascript - & gt; & Lt; Script src = "// ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> & Lt; Script src = "http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.js" & gt; & Lt; / Script & gt; & Lt; Script src = "{% static" js / custom.js "%}" & gt; & Lt; / Script & gt; & Lt; Script src = "{% static" js / jquery.blockUI.js "%}" & gt; & Lt; / Script & gt; {% Load static} {% block content%} {% endblock%}  from <% bootstrap_javascript%} {% load bootstrap3%} {staticfiles%} 

I am unable to reference Csrf_token is available in the current template in the static JS file in Django. How can i work it

If you want to refer to the template tag, then you need that file templates (rendered by Django ) And I do not recommend converting all of your static files through the DEGENGO ...

You can either enter csrf_token in a global variable that you access from your script, it's your base Something is in. Html:

  & lt; Script & gt; Var csrftoken = '{{csrf_token}}'; & Lt; / Script & gt;  

Or you can drag csrftoken from cookies into your javascript file. For a solution, see the cookie is called csrftoken You can see it by opening your Dev Tool and seeing a cookie for your domain.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -