Django Template Variables and Javascript

后端 未结 15 2373
误落风尘
误落风尘 2020-11-22 05:59

When I render a page using the Django template renderer, I can pass in a dictionary variable containing various values to manipulate them in the page using {{ myVar }}

15条回答
  •  悲哀的现实
    2020-11-22 06:19

    Note, that if you want to pass a variable to an external .js script then you need to precede your script tag with another script tag that declares a global variable.

    
    
    
    

    data is defined in the view as usual in the get_context_data

    def get_context_data(self, *args, **kwargs):
        context['myVar'] = True
        return context
    

提交回复
热议问题