当我使用Flask正常运行我的计算机上的Web应用程序时。但是当我将其部署到heroku时,它的行为就开始变得奇怪了。 有问题的应用-app
您会看到有时应用程序未注册您的输入,有时我遇到内部服务器错误,但是如果我不断刷新页面,它将起作用。
看起来有重定向问题,或者会话有问题吗?
使用heroku时我应该做的其他事情吗?
烧瓶
from flask import Flask, render_template, session, redirect, url_for
from flask_session import Session
from tempfile import mkdtemp
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
@app.route("/")
def index():
if "board" not in session:
session["board"] = [[None, None, None], [None, None, None], [None, None, None]]
session["turn"] = "X"
if draw(session["board"]) == True:
return render_template("gameover.html", game=session["board"], draw=draw)
if winner(session["board"]) is not None:
return render_template("gameover.html", game=session["board"], winner=winner(session["board"]))
return render_template("game.html", game=session["board"], turn=session["turn"])
@app.route("/reset")
def reset():
session["board"] = [[None, None, None], [None, None, None], [None, None, None]]
session["turn"] = "X"
return redirect(url_for("index"))
@app.route("/play/<int:row>/<int:col>")
def play(row, col):
session["board"][row][col] = session["turn"]
if session["turn"] == "X":
session["turn"] = "O"
else:
session["turn"] = "X"
return redirect(url_for("index"))
def winner(board):
# check colums
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] and board[i][0] != None:
return board[i][0]
# check rows
for i in range(3):
if board[0][i] == board[1][i] == board[2][i] and board[0][i] != None:
return board[0][i]
# check diagonal
if board[0][0] == board[1][1] == board[2][2] or board[2][0] == board[1][1] == board[0][2] and board[1][1] != None:
return board[1][1]
return None
def draw(board):
if sum(j.count(None) for j in board) == 0 and winner(board) is None:
return True
html
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<style>
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
width: 150px;
height: 150px;
font-size: 30px;
text-align: center;
}
td > a {
font-size: 18px;
}
</style>
</head>
<body>
<table>
{% for i in range(3) %}
<tr>
{% for j in range(3) %}
<td>
{% if game[i][j] %}
{{ game[i][j] }}
{% else %}
<a href="{{ url_for('play', row=i, col=j) }}">Play {{ turn }} here.</a>
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<h1>
<a href="/reset">reset board</a>
</h1>
<h1>
<a href="/computer">let the computer play</a>
</h1>
</body>
</html>
代码https://github.com/amitt1236/tictactoe-
我很乐意提供一些指导
谢谢
答案 0 :(得分:1)
我终于找到了问题。为了在烧瓶中保持会话,您需要使用app.secret_key。
正确的代码:
from flask import Flask, render_template, session, redirect, url_for
from flask_session import Session
from tempfile import mkdtemp
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
app = Flask(__name__)
app.secret_key = "dev"
@app.route("/")
def index():
if "board" not in session:
session["board"] = [[None, None, None], [None, None, None], [None, None, None]]
session["turn"] = "X"
if draw(session["board"]) == True:
return render_template("gameover.html", game=session["board"], draw=draw)
if winner(session["board"]) is not None:
return render_template("gameover.html", game=session["board"], winner=winner(session["board"]))
return render_template("game.html", game=session["board"], turn=session["turn"])
@app.route("/reset")
def reset():
session["board"] = [[None, None, None], [None, None, None], [None, None, None]]
session["turn"] = "X"
return redirect(url_for("index"))
@app.route("/play/<int:row>/<int:col>")
def play(row, col):
session["board"][row][col] = session["turn"]
if session["turn"] == "X":
session["turn"] = "O"
else:
session["turn"] = "X"
return redirect(url_for("index"))
@app.route("/player")
def winner(board):
# check colums
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] and board[i][0] != None:
return board[i][0]
# check rows
for i in range(3):
if board[0][i] == board[1][i] == board[2][i] and board[0][i] != None:
return board[0][i]
# check diagonal
if board[0][0] == board[1][1] == board[2][2] or board[2][0] == board[1][1] == board[0][2] and board[1][1] != None:
return board[1][1]
return None
def draw(board):
if sum(j.count(None) for j in board) == 0 and winner(board) is None:
return True
已添加部分
app = Flask(__name__)
app.secret_key = "dev"