在 Django 中,{} 和 [] 用于传递上下文变量到模板中。
{} 表示字典,可以通过 key-value 的形式来传递变量到模板中。例如:
```python
context = {'name': 'John', 'age': 30}
```
这个 context 可以在模板中使用,通过对应的 key 来获取变量值:
```
<p>My name is {{ name }} and I am {{ age }} years old.</p>
```
[] 表示列表或者元组,也可以用来在模板中传递变量。例如:
```python
context = ['apple', 'banana', 'orange']
```
这个 context 可以在模板中使用,通过索引来获取相应的值:
```
<p>My favorite fruit is {{ context.0 }}.</p>
```
或者可以使用 for 循环将整个列表的值都渲染出来:
```
<ul>
{% for fruit in context %}
<li>{{ fruit }}</li>
{% endfor %}
</ul>
```