尝试使用Flask返回字符串

2024-06-14 03:54:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用render_模板返回字符串,但它只返回else语句。但当我试图在控制台中打印时,它就起作用了。但当我想在金贾使用的时候就不行了

 def healthchecker():
   # health = ""
   for key,value in raw.items():
     if 'properties' in value:
        health = "Yes"
        print(health) 
                                                        
     else:
        health = "None"                                               
        print(health)

   return health                                         
                                           
 return render_template('portlist.html', data = raw, health = healthchecker())

Tags: key字符串in模板forrawreturnvalue
1条回答
网友
1楼 · 发布于 2024-06-14 03:54:10

您可以只传递字典,而不是将函数传递给模板。然后,您可以轻松地使用它,并在html文件中使用jinja进行迭代

return render_template('portlist.html', data=raw)

另外,对于PEP 8,在分配关键字参数时,避免在等号周围使用空格

相关问题 更多 >