简体   繁体   中英

Can not handle python Flask redirected() 'POST' method

Basically, I have written two views for my Flask webpage:

@app.route("/")
def main():

and

@app.route('/', methods=['POST'])
def main_post():

Later on, I have created two more views in an analogical way:

@app.route("/questions")
def questions():

and

@app.route('/questions', methods=['POST'])
def questions_post():

Somehow, my last ['POST'] method does not work at all. Can anyone tell me why? (After sending second ['POST'] there is 'bad request'.)

Here is my code:

@app.route("/")
def main():
    questionPath, answersPath, returnPath, databasePath, testName, amount = setup.setup()
    names = database.getListOfTests(databasePath)
    return render_template('index.html', entries = names)

@app.route('/', methods=['POST'])
def main_post():
    text = request.form['text']
    processed_text = text
    questionPath, answersPath, returnPath, databasePath, testName, amount = setup.setup()
    names = database.getListOfTests(databasePath)
    if not text in names:
        return render_template('index.html', entries = names)
    else:
        questions2, answers2 = database.getFromDatabase(processed_text,databasePath)
        session['messages'] = questions2
        return redirect(url_for('questions'))

@app.route("/questions")
def questions():
    messages = session['messages']
    session['messages'] = messages
    return render_template('index2.html', entries = messages)

@app.route('/questions', methods=['POST'])
def questions_post():
    text2 = request.form['text2']
    processed_text = text2
    print(processed_text)
    return "XD"

And html:

index.html

<form action="." method="POST">
    <input type="text" name="text">
    <input type="submit" name="my-form" value="Send">
</form>

index2.html

<form action="." method="POST">
    <input type="text" name="text2">
    <input type="submit" name="my-form" value="Send2">
</form>

"." is not correct url.

Use empty string action="" or remove action to send form to the same url

<form action="./questions" method="POST">
<input type="text" name="text2">
<input type="submit" name="my-form" value="Send2">

By editing your index2.html action="./view" this would work fine.

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