编辑和更新方法正在运行。但是,如何在编辑现有值时从数据库中检索值

时间:2018-02-19 11:42:03

标签: python flask flask-wtforms flask-mysql

我正在创建小尺寸的Web界面。我在Web界面中创建了编辑按钮。但是,它无法正常工作。它在数据库中创建为新条目。任何人都可以帮助我。还有一个事情是如何在编辑现有值时从数据库中检索值。谢谢高级。

这是Python代码:

class UserForm(FlaskForm):
   type=StringField('type')

@app.route('/newuser', methods=['GET', 'POST'])
   def add_user():

     form = UserForm()
     if form.validate_on_submit():
         user_details = {
        'type': form.type.data

    }
    sqlsession.add(user_details)
    return redirect(url_for('vehicle_type'))
return render_template('vehicletype.html', form=form)

@app.route('/control/edit/<int:id>',methods=['POST','GET'])
def edit(id):
    qry=sqlsession.query(Vehicletype).filter(Vehicletype.id==id).first()
    form = UserForm(request.form, object=qry)
    if form.validate_on_submit():
    form.populate_obj(qry)
    sqlsession.update(qry)
    sqlsession.commit()
    return redirect(url_for('vehicle_type'))
return render_template('vehicletype.html', form=form)

这是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><!--/.row-->
<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 %}

这是vehicletypedetails.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 %}

我一直试图解决这个问题6天。但我找不到任何解决方案。请你帮我一个人。

1 个答案:

答案 0 :(得分:0)

您编辑现有条目的代码的路线为/control/edit/<int:id>,但您用于提交用户更改的表单指向位于/post/vehicletype的其他路线。< / p>

您需要更改您的退货:

 return render_template('vehicletype.html', form=form)

为:

 return render_template('vehicletype.html', form=form, car_id=id)

然后更改您的html代码:

<form role="form" action="/post/vehicletype" method="post">

为:

<form role="form" action="{{ url_for('edit', id=car_id) }}" method="post">

有了这些,您的表单代码将被提交到正确的路由进行处理。

相关问题