Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶-类型错误:'&燃气轮机';在';BaseQuery';和';int';烧瓶炼金术_Python_Sqlalchemy - Fatal编程技术网

Python 烧瓶-类型错误:'&燃气轮机';在';BaseQuery';和';int';烧瓶炼金术

Python 烧瓶-类型错误:'&燃气轮机';在';BaseQuery';和';int';烧瓶炼金术,python,sqlalchemy,Python,Sqlalchemy,所以我试图创建一个“登录”系统,引用的是因为没有安全性 我目前的代码如下: @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': if not request.form['user'] or not request.form['password']: flash('Please enter all the fields', 'e

所以我试图创建一个“登录”系统,引用的是因为没有安全性

我目前的代码如下:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        if not request.form['user'] or not request.form['password']:
            flash('Please enter all the fields', 'error')
        else:
            username = request.form['user']
            password = request.form['password']

            exists = db.session.query(User.user).filter_by(
                user=username)
            if exists > 0:
                logged_in = True
                session['username'] = username
                session['logged_in'] = True
                return index(exists=exists)
            return render_template('login.html', exists=exists)
        return render_template('login.html')
    return render_template('login.html')
但是,我得到了一个错误:

TypeError: '>' not supported between instances of 'BaseQuery' and 'int'
我不明白,因为
存在
应该返回一个整数


任何帮助都会很好,谢谢

exists=db.session.query(User.User).filter\u by(User=username)
是一个BaseQuery对象,当您打印exists时,它将返回SQL查询。您正试图比较
BasQuery
实例和
int
实例,因此出现错误

>>> exists = db.session.query(User).filter_by(username='me')
>>> exists
<flask_sqlalchemy.BaseQuery object at 0x7fbbb7812b70>
>>> type(exists)
<class 'flask_sqlalchemy.BaseQuery'>
>>> type(0)
<class 'int'>
>>> print(exists)
SELECT user.username AS user_username 
FROM user 
WHERE user.username = ?

是什么使您认为
exists
应该是一个整数?但是您实际上没有调用
exists()
方法。您刚刚将查询变量命名为“exists”。我假设exists将返回有多少。“我想不会吧!”丹尼尔·罗斯曼我不确定我必须这么做,我该怎么称呼它呢?在if语句中,您正在将
BaseQuery
的实例与
int
进行比较。如果您的查询有
.all()
,那么如果len(exists)>0,您就可以有
>>> exists = db.session.query(User).filter_by(username='me').all()
>>> exists
[<User 1>]
>>> len(exists)
1
>>> type(len(exists))
<class 'int'>