Ben yeni bir Flask öğrenme öğrencisiyim ve küçük uygulamamda verileri nasıl ileriye aktarabileceğimi anlamaya çalışıyorum. Aşağıdaki kod, geçerli bir hareket yürütüldüğünde "Hata kodu: İşlenmemiş Özel Durum" hatasını döndürür ve neyin yanlış olduğunu gerçekten anlamıyorum. (diğer iki seçenek de çalışır.)Flask: Jinja html şablonunda sözlüğü gösteremiyor
py kodu: 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 kodu:
<table>
{% for key, value in theBoard.iteritems() %}
<h1>Key: {{key}}</h1>
<h2>Value: {{value}}</h2>
{% endfor %}
</table>
Girinti ile bir şeyler ters gitti. ilk kod: theBoard = [{1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', 8: '', 9: ''}] .py dosyasının bir parçasıdır –
Hatalogda aşağıdakileri buldum: File "/home/majaokholm/mysite/flask_app.py", satır 19, test elif theBoard [move]! = '': IndexError: liste endeksi aralık dışı –
tüm oradaydı, şimdi sadece –