如何在烧瓶路径中获取表格数据

时间:2018-09-19 12:02:36

标签: python flask sqlalchemy

我有一个登录路径,用户可以登录到该帐户。我想从另一个路由获取该登录路由的表单数据。但是它总是失败。

@app.route('/attendance', methods = ['GET', 'POST'])
def attendance():
    O_id = request.form.get('O_id')
    print('Welcome', O_id)
    emp_nums = {'1000': 'DEV', '1001': 'ANA'}
    value = emp_nums.get(O_id,"")
    print('team is',value)

    userDetails = Employee_data.query.filter_by(team = value).all()
    return render_template('take_attendance.html', userDetails=userDetails)

我使用这种简单的HTML表单

 <form action="" method="POST" margin="center">
      <label>ID</label>
      <input type="text" name="U_id" value 
      {{request.form.U_id}}">

      <label>Password</label>
      <input type="password" na`enter code here`me="password" value="{{request.form.password}}">

      <button type="submit" class="btn btn-primary">Submit</button>
      </form>

Employee_data模型是

class Employee_data(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    O_id= db.Column(db.String(20), unique=True, nullable=False)
    team = db.Column(db.String(50), nullable=False)
    name = db.Column(db.String(100), nullable=False)
    location = db.Column(db.String(100), nullable=False)

def __repr__(self):
    return 'Employee_data %r' % self.oracle_id,self.name

我只需要获取名称和O_Id

在这里我想念什么吗?

1 个答案:

答案 0 :(得分:0)

这是使用GET或POST时要使用的方法

html

<form method="GET" action="/search" >
    <input type="text" name="make"/>
    <input type="text" name="model"/>
    <input type="submit" value="Submit"/>
</form>

<form method="POST" action="/search_post">
    <input type="text" name="make"/>
    <input type="text" name="model"/>
    <input type="submit" value="Submit"/>
</form>

Python

    # Getting arguements from a GET form
@app.route("/search")
def do_search():
    make = request.args.get('make')
    model = request.args.get('model')
    return "You search for car make: {0}, and car model: {1}".format(make, model)


# Getting arguements from a POST form
@app.route("/search_post", methods = ['POST'])
def do_post_search():
    make = request.form.get('make')
    model = request.form.get('model')
    return "You search for car make: {0}, and car model: {1}".format(make, model)