我正在使用Django框架,我正在尝试生成一个li列表。唯一的问题是生成的每个li包含所有相应的值,而不仅仅是它们按顺序出现的实例。例如:
如果我在下面的示例中打印name
,它会给我:
“埃克森美孚公司国际商业机器公司
我怎样才能打印出“Exxon Mobil Corp.”第一次打电话时和“国际商业机器公司”第二次打电话的时候?
tuple = (('Exxon Mobil Corp.', 'XOM', '102.59', '-0.06'), ('International Business Machines Corp.', 'IBM', '182.56', '0.00'))
name, symbol, last, diff = zip(*tuple)
这是我的代码。这是在我的视图文件中:
c = db.cursor()
c.execute("SELECT `name`,`symbol`,`last`,`diff` FROM `ticker_nyse100` LIMIT 5,1")
data = c.fetchall()
name, symbol, last, diff = zip(*data)
这是在基本的html文件中:
{% for each in data %}
<li class = "tkr_name">
<span class = "comp_info">{{ name }} [{{ symbol }}] </span>{{ last }}
{% if '+' in diff %}
<span class = "up">▲</span> {{ diff }}</li>
{% elif '-' in diff %}
<span class = "down">▼</span> {{ diff }}</li>
{% else %}
<span class = "flat">-</span> {{ diff }}</li>
{% endif %}
{% endfor %}
答案 0 :(得分:1)
目前你正在这样做,创建四个列表:
name, symbol, last, diff = zip(*data)
但是在模板中你不能正确地迭代它们。摆脱拉链线更简单,只需在模板中执行此操作:
{% for name, symbol, last, diff in data %}