原先我是写了两个页面,第二个页面接受 id 通过数据库去查并赋值给 form 表单;代码如下:
@
main.route('/',methods=['GET','POST'])
def index(): //返回数据库所有信息展示给首页
all_info = db.session.query(Info).all()
return render_template('index.html',all_info=all_info)
@
main.route('/editinfo/<int:id>',methods=['GET','POST'])
def editinfo(id):
all_info = db.session.query(Info).all() //当点击按钮后跳出到另一个页面,然后接受 ID ,并在数据库里查赋值表单
info = Info.query.filter_by(id=id).all()
form = Editaddress()
for i in info:
if form.validate_on_submit():
i.public_ip = form.public.data
i.private_ip = form.private.data
i.server_id = form.servid.data
i.address = form.address.data
db.session.add(id)
return redirect(url_for('.index'))
form.public.data = i.public_ip
form.private.data = i.private_ip
form.servid.data = i.server_id
form.address.data = i.address
return render_template('modals.html',form=form)
现在页面点击更换成 bootstrap 模态框,不知道怎么 ID 传给服务端下面是页面代码:
{% for i in all_info %}
<tr>
<td>{{ i.id }}</td>
<td>{{ i.public_ip }}</td> //循环读取 db 返回的值
<td>{{ i.private_ip }}</td>
<td>{{ i.server_id }}</td>
<td>{{ i.address }}</td>
<td class="table-action-hide">
<a href="" class="delete-row"><i class="fa fa-trash-o"></i></a> //点击按钮模态框
<i class="fa fa-pencil" data-toggle="modal" data-target="#myModal"></i>
<div class="modal fade" id="myModal" tabindex="-" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel"><b>Update Information</b></h4>
</div>
<div class="modal-body">
<form class="form" method="POST">
{{ form.hidden_tag() }}
{{ wtf.form_field(form.public) }}
{{ wtf.form_field(form.private) }} //模态框中 body 的表单
{{ wtf.form_field(form.servid) }}
{{ wtf.form_field(form.address) }}
<div style = "text-align:right;">
{{ wtf.form_field(form.submit,class="btn btn-success") }}
</div>
</form>
</div>
</div>
</div>
</div>
</td>
</tr>
{% endfor %}