2017-09-24 69 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_