我使用sqlalchemy在我的数据库中查询ip,当结果返回时,一切正常,如果没有结果返回,我怎么能在表单中保持空白。
错误
AttributeError: 'NoneType' object has no attribute 'Ip'
码
@main.route('/post', methods=['GET', 'POST'])
@login_required
def post():
caseid=Masterlist.query.filter_by(Ip=request.args.get('Ip')).first()
form = RepairForm(request.form)
print request.form
if request.form.get("submit"):
repair = Repair(Ip=form.ip.data,Series=form.series.data,Hostname=form.hostname.data,
ManagerIp=form.managerip.data,Comp=form.comp.data,Discription=form.discription.data,
Model=form.model.data,Location=form.location.data,Box=form.box.data,
Important=form.important.data,Faultype=form.faultype.data,Source=form.source.data,
Subject=form.subject.data,Body=form.body.data,Classify=form.classify.data,
Status=form.status.data,auth_id=current_user._get_current_object().id,
Owner=current_user._get_current_object().username,)
db.session.add(repair)
db.session.commit()
flash('报修成功')
return redirect(url_for('.index'))
form.ip.data=caseid.Ip
form.hostname.data=caseid.Hostname
form.managerip.data=caseid.Managerip
form.comp.data=caseid.Comp
form.model.data=caseid.Model
form.location.data=caseid.Location
form.box.data=caseid.Box
form.classify.data=caseid.Classify
form.series.data=caseid.Series
form.discription.data=caseid.Discription
return render_template('post.html',form=form)
答案 0 :(得分:0)
你可以使用简单的if条件..
@main.route('/post', methods=['GET', 'POST'])
@login_required
def post():
caseid=Masterlist.query.filter_by(Ip=request.args.get('Ip')).first()
if caseid:
form = RepairForm(request.form)
print request.form
if request.form.get("submit"):
repair = Repair(Ip=form.ip.data,Series=form.series.data,Hostname=form.hostname.data,
ManagerIp=form.managerip.data,Comp=form.comp.data,Discription=form.discription.data,
Model=form.model.data,Location=form.location.data,Box=form.box.data,
Important=form.important.data,Faultype=form.faultype.data,Source=form.source.data,
Subject=form.subject.data,Body=form.body.data,Classify=form.classify.data,
Status=form.status.data,auth_id=current_user._get_current_object().id,
Owner=current_user._get_current_object().username,)
db.session.add(repair)
db.session.commit()
flash('报修成功')
return redirect(url_for('.index'))
form.ip.data=caseid.Ip
form.hostname.data=caseid.Hostname
form.managerip.data=caseid.Managerip
form.comp.data=caseid.Comp
form.model.data=caseid.Model
form.location.data=caseid.Location
form.box.data=caseid.Box
form.classify.data=caseid.Classify
form.series.data=caseid.Series
form.discription.data=caseid.Discription
return render_template('post.html',form=form)
else:
pass
答案 1 :(得分:0)
解决方案在这里lol
@main.route('/post', methods=['GET', 'POST'])
@login_required
def post():
# Ip=Ip
# Ip=Ip
# print Ip
# Ip=request.args['Ip']
# Ip=request.args.get('Ip')
caseid=Masterlist.query.filter_by(Ip=request.args.get('Ip')).first()
form = RepairForm(request.form)
if caseid:
print request.form
# print request.form
# if form.validate_on_submit():
# if request.method == "POST":
# if request.form["submit"] == "submitpost":
# if request.form.get("submit", "submit"):
if request.form.get("submit"):
repair = Repair(Ip=form.ip.data,Series=form.series.data,Hostname=form.hostname.data,
ManagerIp=form.managerip.data,Comp=form.comp.data,Discription=form.discription.data,
Model=form.model.data,Location=form.location.data,Box=form.box.data,
Important=form.important.data,Faultype=form.faultype.data,Source=form.source.data,
Subject=form.subject.data,Body=form.body.data,Classify=form.classify.data,
Status=form.status.data,auth_id=current_user._get_current_object().id,
Owner=current_user._get_current_object().username,)
db.session.add(repair)
db.session.commit()
flash('报修成功')
return redirect(url_for('.index'))
form.ip.data=caseid.Ip
form.hostname.data=caseid.Hostname
form.managerip.data=caseid.Managerip
form.comp.data=caseid.Comp
form.model.data=caseid.Model
form.location.data=caseid.Location
form.box.data=caseid.Box
form.classify.data=caseid.Classify
form.series.data=caseid.Series
form.discription.data=caseid.Discription
return render_template('post.html',form=form)
else:
if request.form.get("submit"):
repair = Repair(Ip=form.ip.data,Series=form.series.data,Hostname=form.hostname.data,
ManagerIp=form.managerip.data,Comp=form.comp.data,Discription=form.discription.data,
Model=form.model.data,Location=form.location.data,Box=form.box.data,
Important=form.important.data,Faultype=form.faultype.data,Source=form.source.data,
Subject=form.subject.data,Body=form.body.data,Classify=form.classify.data,
Status=form.status.data,auth_id=current_user._get_current_object().id,
Owner=current_user._get_current_object().username,)
db.session.add(repair)
db.session.commit()
flash('报修成功')
return redirect(url_for('.index'))
return render_template('postmanual.html')