烧瓶:如果变量存在,如何仅渲染_模板

时间:2019-03-05 16:44:09

标签: python flask

我在Flask中有几个变量。变量的数量取决于Excel电子表格具有多少列。

问题在于 render_template 中是否存在这些变量。因此,如果Excel电子表格的列较少,则不会声明某些变量,并且render_template会失败。

我将在重新分配之前将这些变量声明为空。但这会给我Null / None(我必须解决),将其删除后再发布-当我想使代码尽可能快时,听起来不是很好。

在不存在变量的情况下,有什么方法比使用if条件更好地避免“ render_template”?还是比将它们预先分配为空更好?

我的代码:

try: label_a = sheet.cell(0, 0).value
except: pass
try: label_b = sheet.cell(0, 1).value
except: pass
try: label_c = sheet.cell(0, 2).value
except: pass
try: label_d = sheet.cell(0, 3).value
except: pass
try: label_e = sheet.cell(0, 4).value


return render_template('index.html',
    label_a=label_a, label_b=label_b, label_c=label_c, label_d=label_d, label_e=label_e
)

0 个答案:

没有答案
相关问题