2016-05-12 102 views
-1

如何用烧瓶打印字典?Python烧瓶打印键

@app.route("/")  
def hello():  


    templateData = {  

    'canzone': vasco rossi,  
    'destinazione': /home/vasco.mp3  

    }  
    return render_template('main.html', **templateData)  

我的字典是由:

print dizionario: 
    vascorossi:/home/vasco.mp3 
    eros:/home/canzone.mp3 
    etc... 

而且我想这可能是在HTML:

的index.html:

vascorossi - albachiara 
    eros - canzone 
    all the keys and value contents 
+0

你知道神社模板? –

+0

你想迭代你的模板中的字典的键吗?因为你可以在jinja模板中做到这一点。您需要将字典作为模板变量之一传递。 – Bhargav

回答

0

在蟒蛇更新此功能低于版本。

@app.route("/")  
def hello():  
    templateData = {  
     'canzone': vasco rossi,  
     'destinazione': /home/vasco.mp3  
    } 
    updated_dict = {} 
    for key in templateData.keys(): 
     updated_dict[key.decode('utf-8')] = templateData[key.decode('utf-8')].key.decode('utf-8') 
    return render_template('main.html', templateData=updated_dict) 

然后在main.html中添加一段脚本以在您的html页面上显示键值对。

main.html中

... 
{% for key in templateData.keys() %} 
    {{ key }} <br> 
    {{ templateData[key] }} 
{% endfor %} 
... 
+0

是的!这是正确的,但是当我写: @ app.route( “/”) DEF你好(): templateData = { '肯娜妮':dict.keys(), 'destinazione':dict.values() } 返回render_template( 'main.html中',templateData = templateData) 如果__name__ == “__main__”: app.run(主机= '0.0.0.0',端口= 80,调试=真) 它们打印字典中的所有内容以及如何分别打印? templateData [key] templateData [value] templateData [Key] templateData [Value]? –

+0

你可以请更新你的问题,并添加一个你想要的格式化输出? –

+1

@fabriziobianchi检查,我已经解决了unicode错误问题 –