首页 文章

在Django模板中的queryset中循环内部循环

提问于
浏览
0

我有这样的查询集:

hello = Hello.objects.all()

在模板中我会这样做以获取数据:

{% for h in hello %}
   {% for i in h.data %} #data is stored like this ['a', 'b', 'c'] -->  I want to access individual componenet, thus I would do:
      {{i}}
   {% endfor %}
{% endfor %}

但不是像以下那样产生数据:

a 
b
c

它产生 ['a', 'b', 'c']

怎么了?我有理由将数据存储在列表中 . 如何单独访问每个数据 . 谢谢

Purpose: 颜色存储在数据字段中: [black, green, brown] 因此我想实现:

div style="color: black"
div style="color: green"
div style="color: brown"

Edit models.py class Hello(models.Model):user = models.ForeignKey(User)data = models.CharField(max_length = 255)

def __str__(self):
    return "%s's decoration photos" % self.user

2 回答

相关问题