Python Flask Sequence of gets and post

2019-08-11 10:23发布

问题:

Hi im working with python and flask

im trying to make a page that server takes you to page 1

then you send information to the server

then takes you to page 2.

then you send information

and it takes you to page 3.

How whould i do that?

Here is some sample code that i attempted not sure if its remotely close

    @app.route('/',methods=['GET', 'POST'])
def hello():
    if request.method == 'GET':
        return render_template('SelectOption.html');
    elif request.method == 'POST':
        option = request.form['option'];
        if option == "1":
            return render_template('Option1.html', option = option)
            if request.method == 'POST':
                return render_template('charts.html');
        elif option =="2":
            return render_template("Charts.html")
            if request.method == 'POST':
                return render_template('charts.html');

回答1:

This sort of logic should be handled within the template itself, or should be split among multiple pages.

Multiple page example:

@app.route('/1',methods=['GET', 'POST'])
def one():
    if request.method == 'GET':
        return render_template('SelectOption.html');
    elif request.method == 'POST':
        option = request.form['option'];

@app.route('/2',methods=['GET', 'POST'])
def two():
    if request.method == 'POST':
        return render_template('charts.html');
    return render_template('Option1.html', option = option)

@app.route('/3',methods=['GET', 'POST'])
def three():
    if request.method == 'POST':
        return render_template('charts.html');
    return render_template("Charts.html")

Split template example:

@app.route('/', methods=['GET', 'POST']
def root():
    if request.method == 'POST':
        return render_template('post.html');
    return render_template('get.html');

post.html:

<html>
<head><title>Post</title></head>
<body>
    <h1>Hello {{ request.form['username'] }}</h1>
</body>

get.html:

<html>
<head><title>Post</title></head>
<body>
    <form action="/">
    Name:<br> <input type="text" name="username"><br>
    <input type="submit" value="Submit">
    </form>
</body>