#17419: Add a JSON template filter
-------------------------------------+-------------------------------------
     Reporter:  Lau Bech Lauritzen   |                    Owner:  Aymeric
                                     |  Augustin
         Type:  New feature          |                   Status:  new
    Component:  Template system      |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  json template tag    |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by blighj):

 My Two cents on this.
 Escaping json is tricky for all the reasons outlined above, is it in a
 <script> is it in an attribute and will people know what to do with a
 filter that seems to output as safe json, but turns out it is only in
 certain circumstances.

 I would propose that we have a simple filter that outputs the json in a
 <script type="application/json"></script> block
 {{{
 {{ data|as_json_script:"id_data" }}
 }}}

 which would output
 {{{
 <script id="id_data" type="application/json">{"hello": ["world",
 "universe"]}</script>
 }}}
 and which can then be read in by other javascript
 {{{
 var data = JSON.parse(document.getElementById("id_data").textContent)
 }}}

 or similar with jquery etc

 This doesn't leave developers under any illusion that it could be used in
 html attribute or within a script tag and has the added advantage that it
 facilitates moving away from inline javascript which is better for
 supporting good CSP policies.

 Would be happy to put together a pull request if there was support for
 this approach.

--
Ticket URL: <https://code.djangoproject.com/ticket/17419#comment:31>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/061.d8a9660582940ba954de9463020b0598%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to