编辑和更新方法不起作用
Edit and Update Method is Not working
我的问题是,从客户端使用 python-flask 编辑和更新值。我对此一无所知,新的 python-flask 使用 MySQL 数据库。我试过这种编辑和更新的方法 purpose.But,它不是 working.Additionally 当我们输入详细信息并提交时,详细信息将添加到数据库中。任何人帮助我。
这是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代码更新方法无效。但是,当我们提供详细信息时,详细信息将添加到数据库中。如何从客户端编辑和更新值。
以下是使用 wtforms 创建表单的最佳做法
class UserForm(Form):
name = StringField('Name')
father_name = StringField('Father Name')
# .... add fields you want
对于字段类型 - 请参阅此 link
@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)
有了表格后,您就可以轻松编辑内容并将其直接保存到数据库
@app.route('/control/edituser/<int:id>',method=['post','get'])
def edit(id):
qry=sqlsession.query(Enduser).filter(Enduser.id==id).first()
form = UserForm(request.form, **qry)
if form.validate_on_submit():
form.populate_obj(qry)
sqlsession.update(qry)
sqlsession.commit()
return redirect(url_for('page_newuser'))
return render_template('newuser.html', form=form)
我的问题是,从客户端使用 python-flask 编辑和更新值。我对此一无所知,新的 python-flask 使用 MySQL 数据库。我试过这种编辑和更新的方法 purpose.But,它不是 working.Additionally 当我们输入详细信息并提交时,详细信息将添加到数据库中。任何人帮助我。
这是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代码更新方法无效。但是,当我们提供详细信息时,详细信息将添加到数据库中。如何从客户端编辑和更新值。
以下是使用 wtforms 创建表单的最佳做法
class UserForm(Form):
name = StringField('Name')
father_name = StringField('Father Name')
# .... add fields you want
对于字段类型 - 请参阅此 link
@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)
有了表格后,您就可以轻松编辑内容并将其直接保存到数据库
@app.route('/control/edituser/<int:id>',method=['post','get'])
def edit(id):
qry=sqlsession.query(Enduser).filter(Enduser.id==id).first()
form = UserForm(request.form, **qry)
if form.validate_on_submit():
form.populate_obj(qry)
sqlsession.update(qry)
sqlsession.commit()
return redirect(url_for('page_newuser'))
return render_template('newuser.html', form=form)