python - 如何解决 Heroku 部署问题?
问题描述
当我使用烧瓶在我的计算机上运行网络应用程序时,一切正常。但是当我将它部署到 Heroku 时,它开始表现得很奇怪。有问题的应用程序-应用程序
您可以看到有时该应用程序没有注册您的输入,有时我会收到内部服务器错误,但如果我不断刷新页面,它就可以正常工作。
看起来有重定向问题,或者会话有问题?
在使用 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>
解决方案
我终于找到了问题。为了在烧瓶中保持会话,您需要使用 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"
推荐阅读
- git - 为什么我的 HEAD ref 无法在 git 上运行?
- python - 如何在 Python 中创建无符号字符数组?(对于使用 PyOpenGL 的 glReadPixels)
- azure - 建议将已部署的 v2 azure 功能升级到 v3,还是创建新资源并从头开始部署更好?
- python - 如何在pytorch中按第一维对张量进行排序?
- ssas - MDM Cube 浏览快速与 Excel 数据透视太慢
- sql - 删除基于 SQL Server 表中的单个列的所有非重复行,其中 select 语句的多列条件
- jmeter - 如何使用 JMeter 模拟缓慢上升?
- android - 如何将 ImageViews 放在另一个 ImageView 之上?
- c# - 如何将 JsonDocument 反序列化为已知模型?
- css - Internet Explorer 中的边框图像与 Chrome 中的 svg 视图不同,如何解决?