[Django] #33469: Make ID argument of json_script filter optional

10 views
Skip to first unread message

Django

unread,
Jan 28, 2022, 11:55:19 AM1/28/22
to django-...@googlegroups.com
#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.

Django

unread,
Jan 28, 2022, 11:57:16 AM1/28/22
to django-...@googlegroups.com
#33469: Make ID argument of json_script filter optional
---------------------------------+--------------------------------------

Reporter: Adam Johnson | Owner: nobody
Type: New feature | Status: closed

Component: Template system | Version: dev
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Adam Johnson):

* status: new => closed
* resolution: => duplicate


Comment:

What the heck, when I look on the main branch I discover this has already
been done in #33302. Great minds...

--
Ticket URL: <https://code.djangoproject.com/ticket/33469#comment:1>

Reply all
Reply to author
Forward
0 new messages