# Add {% withdict %} and {% withlist %} tags to allow creating dicts and lists in templates

**URL:** <https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658>\
**Category:** Django Internals\
**Created:** [October 14, 2024, 8:57am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658 "2024-10-14T08:57:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 14, 2024, 8:57am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/1 "2024-10-14T08:57:00Z")

</div>

So this one may have been discussed before and I’m sure this feature was rejected before it may look like a antipattern and data should be computed in the view.

However, I feel like there are very valid use cases for creating lists and dicts locally in the template.

Ok, first, there is already the `{% with %} tag allowing to declare variables in context. And there may be use cases where the template needs to transform on or several variables in a list or a dict but pre-computing them in context is just code noise.

This is what I propose:

```django
{% withdict key1=some_variable key2=some_other_variable as my_dict %}
   {{ my_dict.key1 }}
{% endwithdict %}

```

```django
{% withlist variable1 variable2 variable3 as my_list %}
  {% for var in my_list %}
    {% include "some/template.html" with tpl_var=var %}
  {% enfor %}
{% endwithlist %}

```

I understand that `{% cycle %}` may fill the same use that `{% withlist %}` however — and this is personal test here — I don’t fully understand how it work. Plus it may be disturbing to use `{% with %}` to declare a simple var, `{% withdict %}` to declare a dict variable but `{% cycle %}` to declare a list so I think `{% withlist %}` has a place here, may it be just an wrapper around `{% cycle %}`.

---

<div class="post-metadata">

**Author:** ![boxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/boxed/32/11975_2.png) [@boxed](https://forum.djangoproject.com/u/boxed)\
**Post date:** [October 14, 2024, 2:15pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/2 "2024-10-14T14:15:59Z")

</div>

You can build this yourself and if you find it useful a lot, you can suggest it then?

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 14, 2024, 6:35pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/3 "2024-10-14T18:35:07Z")

</div>

I did build it myself and found it useful which is why I propose this addition to the community because I think it may be useful to other ppl.

---

<div class="post-metadata">

**Author:** ![apollo13](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/apollo13/32/30_2.png) [@apollo13](https://forum.djangoproject.com/u/apollo13)\
**Post date:** [October 14, 2024, 6:46pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/4 "2024-10-14T18:46:54Z")

</div>

To be honest I dislike the with-tag already due to the extra indentation it comes with. I’d prefer a syntax along the lines of the set-tag that Jinja uses

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 15, 2024, 5:45am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/5 "2024-10-15T05:45:27Z")

</div>

Sure, me too. But I feel like that should be another discussion entirely.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 15, 2024, 12:44pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/6 "2024-10-15T12:44:03Z")

</div>

Historically these kind of easy user-land tags have been suggested to be project level, simply not to grow the surface area of the built ins too far/indefinitely. It’s similar to Database Functions, which again are quite trivial but almost unbounded in potential number.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 18, 2024, 3:55pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/7 "2024-10-18T15:55:49Z")

</div>

I understand. However, I feel like the current Django templating system often gets in my way. I miss some basic features that Jinja provides:

1. being able to set abitrary variable in context from the template, in particular lists and dict,
2. being able to access items of an object by variable content and not just name, i.e `{{ my_dict[key_var] }}`
3. Being able to control whitespaces in tag like Jinja does with `{{- -}}` and is a especially handicaping when formatting text.

I know that the rationale is to keep the templating language as simple as possible, that it is only target at formatting HTML and that I can use Jinja instead. But Django’s template engine is so dominant in the ecosystem that adopting Jinja for a project is costly.

I really think that Django template engine could greatly benefit from providing some of these features.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 18, 2024, 4:05pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/8 "2024-10-18T16:05:12Z")

</div>

@christophehenry I don’t know what to say. This kind of point is as old as the DTL itself. It’s not going to become Jinja (and nor should it)

All I can think to do is link you to [design philosophies page](https://docs.djangoproject.com/en/5.1/misc/design-philosophies/#template-system).

> Django’s template engine is so dominant in the ecosystem that adopting Jinja for a project is costly.

Yes, I get that. But I’d suggest the reason it’s dominant is (both) that it’s fine, and that the things you miss from Jinja don’t actually matter all that much. (When 1.8 was released a great migration to Jinja was foretold… — needless to say, it never happened.)

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 18, 2024, 5:11pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/9 "2024-10-18T17:11:39Z")

</div>

I still think the current implementation does not meet the current design philosophy. For instance, being unable to control whitespaces generated by tags makes it practically unusable for rendering plain text.

```django
{% for item in items %}
- {{ item }}
{% endfor %}

```

Will generate:

```txt

- one

- two

- three

- four

- five

```

Which is not a text you want to send in an email.

```django
{% for item in items %}- {{ item }}{% endfor %}

```

Will generate `- one- two- three- four- five`. So the only solution you have to get a proper list is either:

```django
{% for item in items %}
- {{ item }}{% endfor %}

```

Which is not easy to read and will probably get flagged by Djade or djLint.

As for being able to declare and manipulate lists from template, it’s just a matter of simplicity and efficiency. I just don’t want to have to always precompute and transform data in the `get_context()`. It’s not always useful (for instance if you determine inthe template if you are going to display the data or not) nor it is handy. For instance, you may want to use the same view, and thus the same `get_context()` with different template that will display the same data differently. Sometimes, only the template knows how it wants the data to look like.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 18, 2024, 5:12pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/10 "2024-10-18T17:12:49Z")

</div>

> [@carltongibson](#):
>
> (When 1.8 was released a great migration to Jinja was foretold… — needless to say, it never happened.)

Can you expand on what happened with 1.8 release? I wan’t even a developer then.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 18, 2024, 5:27pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/11 "2024-10-18T17:27:53Z")

</div>

Support for multiple template backends (basically Jinja) was added

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [October 18, 2024, 9:51pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/12 "2024-10-18T21:51:50Z")

</div>

> [@carltongibson](#):
>
> Historically these kind of easy user-land tags have been suggested to be project level, simply not to grow the surface area of the built ins too far/indefinitely.

+1 here. The DTL isn’t perfect, but you can “DIY” with custom tags and filters. `withdict` and `withlist` seem a bit niche to include as builtins.

> [@carltongibson](#):
>
> Support for multiple template backends (basically Jinja) was added

Yes - see [the release notes](https://docs.djangoproject.com/en/dev/releases/1.8/#multiple-template-engines). They’re all still there!

> [@christophehenry](#):
>
> will probably get flagged by Djade

Not by Djade—I designed it specifically to avoid altering rendered output.

> [@apollo13](#):
>
> I’d prefer a syntax along the lines of the set-tag that Jinja uses

I could get behind a `{% set %}` tag proposal [like Jinja’s](https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments) if we start that discussion.

---

<div class="post-metadata">

**Author:** ![boxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/boxed/32/11975_2.png) [@boxed](https://forum.djangoproject.com/u/boxed)\
**Post date:** [October 19, 2024, 4:01am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/13 "2024-10-19T04:01:55Z")

</div>

I think there’s also a bit of “it’s always been like this” motivation behind some decisions in Django templates that imo does not suit the current community anymore. The idea that templates are written by designers and not programmers is the basic cause. I would hazard a guess that ~zero% non-programmers write Django templates nowadays, even if that did happen 20 years ago at the start.

If we agree that this idea is of the past, and not the present, I think we could move forward.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 19, 2024, 9:52am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/14 "2024-10-19T09:52:00Z")

</div>

> [@adamchainz](#):
>
> I could get behind a `{% set %}` tag proposal [like Jinja’s](https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments) if we start that discussion.

Alright I [started this discussion](https://forum.djangoproject.com/t/add-a-set-tag-supporting-jinja-like-assignements/35787).

---

<div class="post-metadata">

**Author:** ![bllendev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bllendev/32/24699_2.png) [@bllendev](https://forum.djangoproject.com/u/bllendev)\
**Post date:** [October 20, 2024, 5:23am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/15 "2024-10-20T05:23:19Z")

</div>

imo i think reducing complexity in templates will lend to better code, interesting idea though

---

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [October 21, 2024, 2:28pm UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/16 "2024-10-21T14:28:17Z")

</div>

I’m -1 to `withdict` and `withlist`. My reasons:

- This feels like an unpolished API and it does not scale. For example, future users can come and ask for `withset`, `withstr`, etc. that follow this pattern.
  - Also I think this leaks logic into the templates and could potentially confuse new users.

- I have use Django to develop medium and large projects and not once I’ve needed something like this. I know this does not qualify as an objective counterargument, but I think it does show a difference in programming patterns. Perhaps the need of such logic in a template may indicate that a different approach is taken in the views? My preferred style (because it’s more testable, it scales better, it encapsulated logic) is to have these required constructs as either:
  - Model instance (cached) properties (when it makes sense and when the logic belongs in the Model)
  - Context attributes

- Adds extra burden of maintenance and documentation to the project for a non clear general benefit.

To me, isolating business logic to python layers and not having those in the templates is “a must”. Maybe it would help the conversation to read 2-3 use cases for these proposed tags? Those would show what pattern or what approach justifies these tags not violating encapsulation of logic?

Lastly, and as it was suggested in other posts, perhaps this could be first presented as a third party app? “django-extra-tags-and-filters” perhaps?

PS: As a side note, I’ve encountered the need to use the `add` filter beyond numbers. It would be beneficial to enhance it to mimic Python’s `+` operator for a more natural and expected behavior.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 22, 2024, 7:30am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/17 "2024-10-22T07:30:09Z")

</div>

I suppose this thread can be closed, then.
