Search code examples
pythonflaskflask-wtformsflask-mysql

Edit and Update method is working.But,how to retrieve the values from database while editing the existing values


I'm Creating Small Size of Web Interface.I created the edit button in Web Interface.But ,it's not working.It Creating as a new Entry in Database.Can anyone help me.One more thing is How to retrieve the values from database while editing existing values.Thanks for advanced.

Here is The Python Code:

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)

Here is the Templates of the vehicletype.html Code:

  {% 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 %}

Here is the vehicletypedetails.html code:

  {% 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 %}

I have been trying to solve this issue for 6 days .But I Could not find any solution. Please could you help me anyone.


Solution

  • Your code to edit an existing entry has a route of /control/edit/<int:id>, but your form you're using to submit the user-changes is pointing at a different route located at /post/vehicletype.

    You'll need to change your return from:

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

    to:

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

    And then change your html code from:

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

    to:

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

    With that in place, your form code will be getting submitted to the correct route for processing.