无法处理python Flask重定向()' POST'方法

时间:2016-12-06 00:59:53

标签: python html redirect post flask

基本上,我为Flask网页写了两个观点:

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

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

后来,我以类比的方式创建了另外两个视图:

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

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

不知何故,我的上一个['POST']方法根本不起作用。谁能告诉我为什么? (在发送第二个['POST']后,会出现“错误请求”。#)。

这是我的代码:

@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"

和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>

2 个答案:

答案 0 :(得分:1)

"."不正确的网址。

使用空字符串action=""或删除action将表单发送到同一url

答案 1 :(得分:1)

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

通过编辑index2.html action="./view",这样可以正常使用。