Django Template Variables and Javascript

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

    As of Django 2.1, a new built in template tag has been introduced specifically for this use case: json_script.

    https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#json-script

    The new tag will safely serialize template values and protects against XSS.

    Django docs excerpt:

    Safely outputs a Python object as JSON, wrapped in a tag, ready for use with JavaScript.

提交回复
热议问题