2017-08-10 4 views
-1

現在、商品や価格を尋ねる2つのテキスト入力があるシンプルなSuperMarketリストで作業しています。私はこの2つのデータ入力を要求したときにdb Flaskからの整数の要求と表示sqlAlchemy

Image of the both textfields

Image of the program without Price Textfield

だから、それは私にデータを検証するものです私のスーパールートでエラーが発生します。ここで

@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('/') 

私はそれを呼び出すと、後でそれを表示することができますので、データベースからデータを要求するために価格を追加しましたが、私はエラーを取得する場所です。

これは私のDBがセットアップされているか:

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

缶:

super = Super(content, precio) 

ビットを推測、何を後にしたことは、おそらくこのようなものですあなたはエラーメッセージを投稿しますか? – Nabin

+0

はい、コンソールで私はこのエラーを受け取ります.. 127.0.0.1 - - [10/Aug/2017 12:50:41] "POST/super HTTP/1.1" 500 - データは「内部サーバーエラー」に移動します –

答えて

0

あなたSuperは、その__init__で2つのパラメータがかかりますが、あなただけの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) 

これは、あなたが行うときに何が起こるか、おそらく次のとおりです。

super = Super(content) 

ただし、2つのパラメータ:

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

だから、あなたはおそらく意図したように、二つの引数でSuperインスタンスを作成する必要があります。

@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

関連する問題