I\'m writing a small web app with flask. I have a problem with session variables when two users (under the same network) try to use app.
This is the code:
So you do something like this. It is not tested but should work. You retrieve the current username and the numbers dictionary from the session variable. Check if there is an existing value for the current username. If not create a random number and save it in the session. Else just use the saved value.
@app.route('/check', methods=['GET', 'POST'])
def check():
# retrieve current username and numbers from your session
username = session['username']
numbers = session.get('numbers', {})
# if no number is existing create a new one and save it to session
if username not in numbers:
number = randint(0,4)
numbers['username'] = number
session['numbers'] = numbers
else:
number = numbers['username']
return render_template('file.html', number=number, user=username)