我有一个表名叫“代码”和3列:ID(主),码号(串)和激活(布尔值,默认值= FALSE)如何检查数据库中的现有值并更改它?
,我已经在这里我要检查值的形式,如果它存在,使其激活== False。
我想: myform.py:
#all imports here
class CheckForm(Form):
some_code = StringField('Code: ',validators=[Required()])
submit = SubmitField('Check it!')
我views.py:
#all imports here
@some.route('/sometest', methods=['GET', 'POST']
def check_function():
form = CheckForm()
code=form.some_code.data
check_code = Codes.query.filter_by(code=code).first()
if check_code.activated == False:
check_code.activated = True
db.session.add(check_code)
db.session.commit()
elif check_code.activated == True:
print 'Code already used'
else:
print 'Code not found')
return render_template('test.html')
但我发现了错误:
AttributeError: 'NoneType' object has no attribute 'activated'
我m使用烧瓶和sqlalchemy
您的对象'check_code'为空,这意味着数据库中没有包含您的条件的行。 – badc0re 2014-11-05 10:46:42