簡體   English   中英

Flask:無法在Jinja HTML模板中顯示字典

[英]Flask: unable to display dictionary in Jinja html template

我是一個學習Flask的新手,正在嘗試弄清楚如何在我的小型應用程序中來回傳遞數據。 當播放有效的舉動時,下面的代碼返回錯誤“錯誤代碼:未處理的異常”,但我真的不知道出什么問題了嗎? (其他兩個選項都可以正常工作。)

py代碼:theBoard = [{1:'',2:'',3:'',4:'',5:'',6:'',7:'',8:'',9:' '}]

@app.route('/test', methods=["GET", "POST"])
def test():
    if request.method == 'POST':
        x = request.form['move']
        move = int(x)
        valid_moves = [1,2,3,4,5,6,7,8,9]
        if move not in valid_moves:
            return 'you did not specify a valid move, please try again!'
        elif theBoard[move] != ' ':
            return 'you can not play that space, it is taken'
        else:
            theBoard[move] = 'X'
            return render_template("test2.html", theBoard=theBoard)

    return render_template("test.html")

html代碼:

<table>
{% for key, value in theBoard.iteritems() %}
<h1>Key: {{key}}</h1>
<h2>Value: {{value}}</h2>
{% endfor %}
</table>

theBoard是一個列表,在test()和html模板中,您將其視為字典。 用下面的代碼替換第一行,然后查看是否有效。

theBoard = {1:' ', 2:' ', 3:' ', 4: ' ', 5:' ', 6: ' ', 7:' ', 8:' ', 9:' '}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM