There are two vyushki. One gets an input from the user on the page, creates a dictionary, draws the page and gives the data to the created dictionary to another view that works further with it. How can I transfer a dictionary from one input to another.

@app.route("/game/") def game(): # сюда я должен его получит и проделать с ним манипуляцииreturn render_template('game.html', data=data) @app.route("/") def page(): num = request.args.get("num") if num: data = {"field": get_field(int(num)), "num": int(num),"chord": "0", "empty": '.', 'x': 'x', 'o': 'o', "moves": 1} return render_template('game.html', data=data) # эта создаёт словарь return render_template('first_page.html') 

And provided that in HTML I work through links: by type:

 {% else %} <span><a href="{{ url_for('game')}}"> <img src="{{ url_for('static', filename='img/main.png') }}" width='30px' height='15px'/></a> 

3 answers 3

If /game always requires parameters from you, then you can explicitly pass them in the form of url parameters ( /game?num=1&chord=0 ):

 @app.route('/game') def game(): data = dict(num=request.args.get('num'), chord=request.args.get('chord')) return render_template('game.html', data=data) @app.route('/') def index(): num = request.args.get('num') if num: return redirect(url_for('game', num=int(num), chord='0')) return render_template('index.html') 

Where templates/index.html :

 <!doctype html> <title>Index</title> <form> <input name="num"> <input type="submit"> </form> <a href="{{ url_for('game', num=1, chord='0') }}">game</a> 

and templates/game.html :

 <!doctype html> <title>Game</title> <dl> <dt>Num <dd>{{ data.num }} <dt>Chord <dd>{{ data.chord }} </dl> 

If you want to implicitly transmit the state generated on the server, you can use the session (it is passed between the requests in cookies):

 #!/usr/bin/env python from flask import Flask, request, render_template, redirect, url_for, session app = Flask(__name__) @app.route('/game') def game(): return render_template('game.html', data=session.get('data') or {'num': 1, 'chord': '0'}) @app.route('/') def index(): num = request.args.get('num') if num: session['data'] = dict(num=int(num), chord='0') return redirect(url_for('game')) return render_template('index.html') app.secret_key = '73870e7f-634d-433b-946a-8d20132bafac' if __name__ == '__main__': app.run(host='localhost', port=3000, debug=True) 

where templates/index.html :

 <!doctype html> <title>Index</title> <form> <input name="num"> <input type="submit"> </form> <a href="{{ url_for('game') }}">game</a> <!-- data is from the current session --> 

and templates/game.html the same.

    Use flask.g

     from flask import g @app.route('/') def page(): ... # Ваш код g.data = data @app.route('/game/') def game(): data = getattr(g, 'data', None) if data is not None: ... # Ваш код else: ... # Ваш код в случае ошибки 


    You can also use redirect(url_for('game', data=data))

    In this case, you need in the parameters to the game function to pass the data argument

     def game(data=None): if data is not None: ... # Ваш код else: ... # Ваш код в случае ошибки 
    • both solutions are wrong. 1- g between requests is not saved, so the data you always None . 2- redirect(url_for('game', data=data)) will transmit data as in url query, so again you always have data None . - jfs
    • @jfs the question was about one request context, not about several, but at the expense of the second you are probably right - Mr Morgan
    • already the title of the question itself more than one request implies (more than one "view"). Try to create a minimal, but complete code example with g.data and make sure that it does not work (a lot of code is not required here, look at the full example in my answer ). - jfs

    Mr Morgan, thanks for telling me where to look. Solved with g and global variables. Maybe I'm tight, but I spent all day with this moment. Can someone come in handy ....

     from flask import render_template, request, redirect, url_for, flash, g FIELD = [] NUM = 0 @app.route("/") def page(): num = request.args.get("num") if num: global NUM NUM = int(num) global FIELD FIELD = get_field(NUM) return render_template('game.html', data=data) # эта создаёт словарь return render_template('first_page.html') @app.route("/game<pk>/<move>/<line>:<point>") def game(pk, move, line, point): game_playing = Game.query.filter(Game.id == pk) if move == 0: field = getattr(g, 'field', None) else: field = FIELD return render_template('game.html', field=field, data=DATA, pk=pk) 

    and yet, as it turned out, there should be such a thing, without which g will not work

     @app.before_request def before_request(): g.field = get_field(NUM) 
    • there are too many broken to list. Start by correcting indents. - jfs