Django Template Variables and Javascript

后端 未结 15 2773
误落风尘
误落风尘 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:08

    you can assemble the entire script where your array variable is declared in a string, as follows,

    views.py

        aaa = [41, 56, 25, 48, 72, 34, 12]
        prueba = ""
    

    that will generate a string as follows

    prueba = ""
    

    after having this string, you must send it to the template

    views.py

    return render(request, 'example.html', {"prueba": prueba})
    

    in the template you receive it and interpret it in a literary way as htm code, just before the javascript code where you need it, for example

    template

    {{ prueba|safe  }}
    

    and below that is the rest of your code, keep in mind that the variable to use in the example is data2

    
    

    that way you will keep the type of data, which in this case is an arrangement

提交回复
热议问题