-1

Each time i click on button i would like to add some value to string and present it inside div.

In the free time i try to write simple text adventure game in Python with Flask. Somehow after 4-5 clicks on buttons the value of the "view" resets. App is running on Pythonanywhere

Python/Flask

from flask import Flask, render_template, request

app = Flask(__name__)


game_data={
          'view':'',
          'action' : None
          }


rooms=['Walls and ceiling made of black stone.']

@app.route('/')
def home():
    return render_template('index.html',game_log_div=game_data['view'])


@app.route('/',methods=['POST'])
def update():
    game_data['action'] = request.form['action']
    app.logger.error('game_view: '+game_data['view'])
    if game_data['action'] == 'look':
        game_data['view']+=rooms[0]
    else:
        game_data['view']+='can\'t'

    game_data['action'] = None
    return render_template('index.html',game_log_div=game_data['view'])

HTML/Template

<html>                                                                                                                                                           
<head>
<title>ADVENTURE-SHELL</title>
</head>
<body>
    <div id='game_log'>{{game_log_div}}</div>
    <div id='room_buttons'>
         <form method="POST" action="/">
             <input type="submit" name="action" value="look">
             <input type="submit" name="action" value="go">
        </form>
    </div>
</body>
</html>

server log :

flask_app: game_view:

flask_app: game_view: Walls and ceiling made of black stone. Silver and gemstone inlays represent stars and planets in a beautiful cosmic map.

flask_app: game_view: Walls and ceiling made of black stone. Silver and gemstone inlays represent stars and planets in a beautiful cosmic map.can't

flask_app: game_view:

1 Answers1

1

Using flask.session in place of game_data fixed the problem.

davidism
  • 121,510
  • 29
  • 395
  • 339