How can I pass my context variables to a javascript file in Django?

前端 未结 3 2009
庸人自扰
庸人自扰 2020-12-02 14:50

This question must be obvious but I can\'t figure it out.

In a template, I link to a js file in my media directory. From that file, I would like to access a context

3条回答
  •  春和景丽
    2020-12-02 15:03

    I don't think it's possible this way. If you want to access some data provided by the view, you have to pass it to the js function.

    Example

    js file:

    my_cool_js_function(some_param){
        // do some cool stuff
    }
    

    view

    // some html code
    
    my_cool_js_function({{param}})
    

    hope this helps :)

提交回复
热议问题