#33469: Make ID argument of json_script filter optional
-------------------------------------------+------------------------
Reporter: Adam Johnson | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
Currently the `json_script` filter requires an ID. This is burdensome in
loops, as one then has to unique ID's, perhaps in the view.
{{{
def charts(request):
data_sets = ...
charts = [
{"id": f"chart-data-{i}", "data": data}
for i, data in enumerate(data_sets)
]
return render(request, "charts.html", {"charts": charts})
}}}
…and use them in the template:
{{{
{% for chart in charts %}
<my-chart data-id="{{ chart.id }}">
{{ chart.data|json_script:chart.id }}
</my-chart>
{% endfor %}
}}}
If `json_script` did not need an ID, this work could be saved. The
`<script>` tag could avoid having an `id` attribute, and JavaScript could
still look up the `<script>` tag based on its position in the DOM, with
e.g. `this.element.querySelect("script[type='application/json']")`.
The template could then be simpler, and no view logic would be required:
{{{
{% for chart in charts %}
<my-chart>
{{ chart.data|json_script }}
</my-chart>
{% endfor %}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33469>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.c71f905a56ac3c7a8aa2a6665f6e8456%40djangoproject.com.