You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/templates.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,57 @@ class HelloWorldView(UnicornView):
65
65
[Django models](django-models.md) has many more details about using Django models in `Unicorn`.
66
66
```
67
67
68
+
## Models inside `{% for %}` loops
69
+
70
+
When iterating over a list with a Django `{% for %}` loop, the loop variable is **not** the same as the component attribute that holds the list. Unicorn syncs `<input>` values by looking up the `unicorn:model` name in the component's serialised data, so the model name must use the **component attribute path** (e.g. `items.0.name`), not the loop variable name (e.g. `item.name`).
71
+
72
+
Use `{{ forloop.counter0 }}` to build the correct index-based path:
The key is `items.{{ forloop.counter0 }}.name` this renders to `items.0.name`, `items.1.name`, etc., which Unicorn can resolve directly against the component data.
108
+
109
+
```{warning}
110
+
Using the **loop variable name** in `unicorn:model` (e.g. `unicorn:model="item.name"`)
111
+
will **not** work correctly after a re-render. When the component re-renders,
112
+
Unicorn tries to look up `item` in the component's data, but `item` is only a
113
+
Django template loop variable — it has no corresponding key in the serialised
114
+
component state. As a result morphdom will clear the input's value.
0 commit comments