2017-08-07 38 views
1

我想用烧瓶从html窗体中获取数据。表单显示在网站上,但是当我提交python没有收到它。使用烧瓶接收来自表格的数据

HTML代码:

<form action="/signup" method="post"> 
    <input type="text" name="email"></input> 
    <input type="submit" value="signup"></input> 
</form> 

Python代码:

from flask import Flask, render_template, request, redirect 
app = Flask(__name__) 
@app.route('/') 
def home(): 
    return render_template('home.html') 
@app.route('/about/') 
def about(): 
    return render_template('about.html') 
@app.route('/signup', methods = ['POST']) 
def signup(): 
    email = request.form['email'] 
    print("The email address is '" + email + "'") 
    return redirect('/') 
if __name__ == '__main__': 
    app.run(debug=True) 

APP

--static

----的main.css

--templates

---- HTML代码(home.html做为)

Python代码(hello.py)

+3

什么是例外? – stamaimer

+0

哪里给出了html代码? – Fejs

+0

将编辑显示代码位于 – Daniel

回答

0

使用瓶对表单动作url_for可以肯定的是submited到正确的位置。

<form method="post" action="{{ url_for('signup') }}"> 
+0

我需要对我的Python代码进行任何更改吗?我试过但得到结果。 – Daniel

+0

不,只要确保在url_for中调用的函数存在于你的路由器 – DobleL

+0

请你能解释我将如何检查? – Daniel

0

该代码适用于我。 我只会改变使用url_for()构造,而不是直接指定您的端点。 I.E .:使用return redirect(url_for('home'))

或者,尝试为您的'注册'端点提供GET请求。

  1. 增加 'GET' 到你的方法参数
  2. 将您的打印块下if声明(if request.method == "POST"
  3. 在此之下块装上去return。 (return redirect(url_for('home')),return render_template('xyz.html')等)