我的问题是,从客户端使用python-flask编辑和更新值。我对使用MySQL数据库的那个和新的python-flask没有任何了解。我尝试了这种方法进行编辑和更新。但是,它不起作用。此外,当我们输入详细信息并提交时,详细信息将添加到数据库中。有人帮助我。
这是vehicletype.html模板。
{% extends "base.html" %}
{% block head %}
{{super()}}
{% endblock %}
{% block navbar %}
{{super()}}
{% endblock %}
{% block content %}
<div class="row">
<ol class="breadcrumb">
<li><a href="#">
<em class="fa fa-home"></em>
</a></li>
<li class="active">Vehicletype > Create Vehicletype</li>
</ol>
</div>
<div class="row">
<div class="col-md-6">
<form role="form" action="/post/vehicletype" method="post">
<div class="form-group">
<label>VehicleType: </label>
<input name="type" class="form-control" placeholder="enter vehicletype">
</div>
<input type="submit" class="btn btn-primary" value="Submit ">
<input type="reset" class="btn btn-default" value="Reset">
</form>
</div>
</div>
{% endblock %}
这是details.html
{% extends "base.html" %}
{% block head %}
{{super()}}
{% endblock %}
{% block navbar %}
{{super()}}
{% endblock %}
{% block content %}
<div class="row">
<ol class="breadcrumb">
<li><a href="#">
<em class="fa fa-home"></em>
</a></li>
<li class="active">Vehicletype>View</li>
</ol>
</div><!--/.row-->
<div class="row">
<div class="col-md-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
Id
</th>
<th>
VehicleType
</th>
<th>
Dateofsub
</th>
<!--<th>
Control
</th>-->
<th>
Delete
</th>
</tr>
</thead>
{% for values in vehicletype %}
<tr>
<th>{{values.id}}</th>
<td>{{values.type}}</td>
<td>{{values.dateofsub}}</td>
<!--<td><a href="/Control/resetuserpass/{{values.id}}" class="btn btn-info">Reset Password</a></td>-->
<td><a href=" /vehicletype/deleteuser/{{values.id}}" class="btn btn-danger">Delete</a></td>
<td><a href=" /control/edit/{{values.id}}" class="btn btn-danger">edit</a></td>
</tr>
{% endfor %}
</table>
<a href = "/page/vehicletype"> <em class="fa fa-xl fa-plus-circle color-blue" ></em> </a>
</div>
</div>
{% endblock %}
编辑方法的python代码:
class VehicetypeForm(FlaskForm):
type=StringField('Type')
@app.route('/control/edit/<int:id>',methods=['POST','GET','PATCH'])
def edit(id):
form = VehicetypeForm(request.form)
mysql = pymysql.connect("0.0.0.0", "tnxt", "tnxt", "transport")
cur = mysql .cursor()
cur.execute('SELECT * FROM vehicletype WHERE id= %s',[id])
type=cur.fetchall()
# form.type.data=type
if request.method=='PATCH' and form.validate():
#type=form.type.data
mysql = pymysql.connect("0.0.0.0", "tnxt", "tnxt", "transport")
cur=pymysql .cursor()
cur.execute('UPDATE vehicletype SET type=%s WHERE id=%s',(type,id))
mysql.connection.commit()
cur.close()
flash('success')
return redirect(url_for('vehicle_type'))
return render_template('vehicletype.html',form=form)
在这个python代码中,更新方法不起作用。但是,当我们提供详细信息时,将在数据库中添加详细信息。如何编辑和更新客户端的值。
答案 0 :(得分:1)
以下是使用wtforms创建表单的好习惯
function resizeSlider() {
var api = revapi1;
var windowHeight = $(window).height();
var height_left = windowHeight - $('header.site-header').height() - $('#uid_581031dff187267690e9823b9a5b565a').height();
var sliderSettings = api.data('opt') || api[0].opt;
//console.log(sliderSettings);
setTimeout(function() {
sliderSettings.height = height_left;
sliderSettings.conh = height_left;
sliderSettings.ulh = height_left;
sliderSettings.gridheight[0] = height_left;
},250);
}
$(window).resize(function(){
resizeSlider();
});
resizeSlider();
表示字段类型 - 请参阅此link
class UserForm(Form):
name = StringField('Name')
father_name = StringField('Father Name')
# .... add fields you want
获得表单后,您可以轻松编辑内容并直接将其保存到数据库
@app.route('/newuser', methods=['GET', 'POST'])
def add_user():
form = UserForm()
if form.validate_on_submit():
user_details = {
name: form.name.data,
fathername: form.father_name.data,
# add more fields
}
sqlsession.add(user_details)
return redirect(url_for('page_newuser'))
return render_template('newuser.html', form=form)