2017-08-10 86 views
-1

我目前在一個簡單的超級市場列表中工作,你有2個文本輸入,他們問產品和價格。 Image of the program without Price Textfield請求和顯示一個整數從數據庫燒瓶sqlAlchemy

Image of the both textfields

所以,當我問這2個數據輸入它給了我在我的超級路線的錯誤是驗證數據的一個。

@app.route('/super', methods=['POST']) 
def add_super(): 
    content = request.form['content'] 
    #precio = request.form['precio'] 
    if not request.form['content'] or not request.form['precio']: 
     flash('Debes ingresar un texto') 
     return redirect('/') 
    super = Super(content) 
    #super = Super(precio) 
    db.session.add(super) 
    db.session.commit() 
    flash('Registro guardado con exito!') 
    return redirect('/') 

這裏我加入了價格從數據庫中請求數據,這樣我就可以把它和後來表現出來,但是這是我的錯誤。

這是我的數據庫是如何設置:

class Super(db.Model): 
    id = db.Column(db.Integer, primary_key=True) 
    content = db.Column(db.Text) 
    precio = db.Column(db.Integer) 
    listo = db.Column(db.Boolean, default=False) 

    def __init__(self, content,precio): 
     self.content = content 
     self.precio = precio 
     self.listo = False 

    def __repr__(self): 
     return '<Content %s>' % self.content 
    # def __repr__(self): 
    #  return '<Precio %s>' % self.precio 
db.create_all() 
+0

能您發佈錯誤消息? – Nabin

+0

是的,在控制檯我得到這個錯誤.. 127.0.0.1 - - [10/Aug/2017 12:50:41]「POST/super HTTP/1.1」500 - 在我填充文本字段後數據轉到「內部服務器錯誤」 –

回答

0

Super需要兩個參數在其__init__,但只提供1個參數。

見例如會發生什麼,如果我們在解釋定義:

>>> class c1(object): 
...  def __init__(self, p1, p2): 
...   self.p1 = p1 
...   self.p2 = p2 

然後,我們嘗試使用只有一個參數來創建一個實例:

>>> c1('spam') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: __init__() takes exactly 3 arguments (2 given) 

這可能是當你發生了什麼:如果我們提供兩個參數:

>>> c1('spam','eggs') 
<__main__.c1 object at 0x7f64a2f3a350> 

所以,你需要創建Super實例有兩個參數,就像你可能預期:

super = Super(content, precio) 

投機一點,你是什麼之後大概是這樣的:

@app.route('/super', methods=['POST']) 
def add_super(): 
    content = request.form.get('content') 
    precio = request.form.get('precio') 
    if precio is None or content is None: 
     flash('Debes ingresar un texto') 
     return redirect('/') 
    super = Super(content, precio) 
    db.session.add(super) 
    db.session.commit() 
    flash('Registro guardado con exito!') 
    return redirect('/') 
+0

噢好吧!謝謝您的幫助!!它工作 –

+0

@CarlosNavarro很高興它幫助,你會標記爲答案呢? – bgse