从数据库中请求并显示一个整数

2024-09-27 04:26:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前在一个简单的超市名单工作,你有2个文本输入,他们问的产品和价格。 Image of the program without Price Textfield

Image of the both textfields

所以当我要求这两个数据输入时,它在我的超级路由中给了我一个错误,那就是验证数据的错误。你知道吗

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

Tags: 数据imageselfformdbreturnrequestdef
1条回答
网友
1楼 · 发布于 2024-09-27 04:26:13

您的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)

但是,如果我们提供两个参数:

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

相关问题 更多 >

    热门问题