我使用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` 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(...)
总结 以上是内存溢出为你收集整理的如何更改Flask-peewee中的UserDoesNotExist SELECT行为 – python和mysql全部内容,希望文章能够帮你解决如何更改Flask-peewee中的UserDoesNotExist SELECT行为 – python和mysql所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)