简体   繁体   中英

How do I solve Heroku deployment problem?

When I'm running the web app on my computer using flask everything in OK. But when I deploy it to Heroku it's starting to behave strangely. The app in question- app

You can see that sometimes the app doesn't register your inputs and sometimes I'm getting internal server error, but if I keep refreshing the page it's working.

Looks like there's a redirect problem, or maybe something with the sessions?

There's something I'm missing you should do when using Heroku?

Flask


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>

The code https://github.com/amitt1236/tictactoe-

I found the problem finally. in order to maintain session in flask you need to use app.secret_key.

the correct code:

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    

part added


app = Flask(__name__)
app.secret_key = "dev"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM