2017-09-24 49 views
-1

我试图从网站表单添加信息到postgresql数据库,当我按下提交按钮时,电子邮件值正确写入数据库但高度值等于[null]? 这里是我的Python脚本:“[none]”value added to postgresql

from flask import Flask, render_template, request 
from flask.ext.sqlalchemy import SQLAlchemy 


app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:[email protected]:5433/height_collector' 
db = SQLAlchemy(app) 


class Data(db.Model): 
    __tablename__ = "data" 
    id = db.Column(db.Integer, primary_key=True) 
    email_ = db.Column(db.String(120), unique=True) 
    height_ = db.Column(db.Integer) 

    def __init__(self, email_, height_): 
     self.email_ = email_ 
     self.heigth_ = height_ 


@app.route("/") 
def index(): 
    return render_template("index.html") 


@app.route("/success", methods=['post']) 
def success(): 
    if request.method == 'POST': 
     email = request.form["email_name"] 
     height = request.form["height_name"] 
     print(email, height) 
     try: 
      data = Data(email, height) 
      db.session.add(data) 
      db.session.commit() 
     except: 
      print('FILE') 
     return render_template("success.html") 


if __name__ == '__main__': 
    app.debug = True 
    app.run() 

这里是我的html tamplate:

 <form action="{{url_for('success')}}" method = "POST"> 
      <input title="Yor email will be safe with us" placeholder="Enter your email address" type="email" name="email_name" required><br> 
      <input title="Yor data will be safe with us" placeholder="Enter your height in cm" type="number" name="height_name" min="50" max="300" required><br> 
      <button type="submit">Submit</button> 
     </form> 
    </div> 

database

回答

2

您的init方法设置为self.heigth_而不是self.height_