Django模板:需要2个值才能解开for循环;拿到8 [英] Django template : Need 2 values to unpack in for loop; got 8

查看:40
本文介绍了Django模板:需要2个值才能解开for循环;拿到8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我的视图,我收集了一些要捆绑在一起的数据,这些数据看起来像这样:

Through my view I collected some data that I want to bundle together in a list of values that look like this :

data = [(1,2,3,4,5,6,7,8),(1,2,3,4,5,6,7,8),(1,2,3,4,5,6,7,8)]

然后,我将其呈现到模板中以将数据解压缩到我的页面中:

Then I'll be rendering that to my template to unpack the data to my page :

return render(request, 'accounts/page.html', {'data' : data})

模板如下所示:

{% for a,b,c,d,e,f,g,h in data %}
    <h3>{{a}}</h3>
    <h3>{{b}}</h3>
    #and so on
    #..
    <h3>{{h}}</h3>
{% endfor %}

所以我得到的错误是:

需要2个值才能解开for循环;拿到了8.

Need 2 values to unpack in for loop; got 8.

任何人都可以找出此错误的根源,或者有更好的方式打包显示数据吗?

Can anyone figure out the source of this error or maybe have a better way of rendering data in bundles ?

谢谢!

推荐答案

请注意不要使用 list 作为变量,因为它是Python类型对象(保留),因此已弃用.
而且问题仅在您的渲染中:

Be careful to not use list as variable it is depreciated since it is a Python Type Object(reserved)
And the problem is only in your render:

my_list = [(1,2,3,4,5,6,7,8),(1,2,3,4,5,6,7,8),(1,2,3,4,5,6,7,8)]
return render(request, 'accounts/page.html', {'my_list': my_list})

这篇关于Django模板:需要2个值才能解开for循环;拿到8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆