Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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
如何在Flask peewee-python&;mysql_Python_Mysql_Json_Flask_Peewee - Fatal编程技术网

如何在Flask peewee-python&;mysql

如何在Flask peewee-python&;mysql,python,mysql,json,flask,peewee,Python,Mysql,Json,Flask,Peewee,我用它来制作一个API,如果表中不存在用户,我想返回一个404 json,但它似乎抛出了500个错误,而不是404错误json: 下面是我得到的错误: UserDoesNotExist: instance matching query does not exist: SQL: SELECT t1.`id`, t1.`username`, t1.`password`, t1.`email`, t1.`token`, t1.`join_date`, t1.`active`, t1.`admin` F

我用它来制作一个API,如果表中不存在用户,我想返回一个404 json,但它似乎抛出了500个错误,而不是404错误json:

下面是我得到的错误

UserDoesNotExist: instance matching query does not exist:
SQL: SELECT t1.`id`, t1.`username`, t1.`password`, t1.`email`, t1.`token`, t1.`join_date`, t1.`active`, t1.`admin` FROM `user` AS t1 WHERE ((t1.`active` = %s) AND (t1.`username` = %s))
[Wed Sep 18 20:26:24 2013] [error] [client 173.225.41.154] PARAMS: [True, u'fery3']
代码:

@app.route('/api/login/',  methods=['POST'])
def authenticate1():
    if request.method == 'POST' and request.form['username']:
        active = User.select().where(User.active==True)
        user2 = User.select().where(User.username==request.form['username']).get()
        try:
            user = active.where(User.username==request.form['username']).get()
        except exceptions:  # includes simplejson.decoder.JSONDecodeError
            return make_response(jsonify( { 'error': 'Bad request' } ), 400)
        else:
            if not user.check_password(request.form['password']):
                return make_response(jsonify( { 'error': 'Bad request','message': 'Wrong Username or Password'} ), 400)
        return make_response(jsonify( { "meta":{'model':'login','code':'200','status':'ok','message':'successfully loggedin'},'objects':[{'username': user2.username,'email':user2.email,'token' : user2.token }] } ), 200)
active = User.select().where(User.active == True)
try:
    user = active.where(User.username == request.form['username']).get()
except User.DoesNotExist:
    return make_response(jsonify({'error': 'Bad request'}), 400)
else:
    if not user.check_password(...)