2016-12-06 117 views
0

基本上,我已经写了我的瓶网页的两个观点:无法处理蟒蛇瓶重定向()“POST”的方法

@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> 
+0

下次使用按钮'{}'来格式化代码。 – furas

+0

总是显示有问题的完整错误消息。文字'坏请求'是没用的。在调试模式下运行 - 'app.run(debug = True)' - 在浏览器中获取更多信息。 – furas

+0

你确定它正在提交到正确的网址吗?让'url_for'建立你的URL而不是对它们进行硬编码通常是一个好主意。 – dirn

回答

1

"."是不正确的网址。

使用空字符串action=""或删除action发送形式相同url

+0

非常感谢! ;) – MrAker

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

通过编辑index2.html action="./view"这将很好地工作。