Falcon bliss
Falcon bliss

Reputation: 71

Edit and Update Method is Not working

My problem is, edit and update the values by using python-flask from client side. I don't have any idea about that and new python-flask using MySQL database. I tried this method for edit and update purpose.But, it's not working.Additionally the details will be added in database when we enter details and submit. anyone help me.

Here is vehicletype.html template.

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

Here is the 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 code for Edit method:

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)

In this python code update method is not working. But, when we give the details that details will be added in database. How to edit and update the values from client side.

Upvotes: 0

Views: 1392

Answers (1)

nicolom
nicolom

Reputation: 360

Below is the good practice to create form using wtforms

class UserForm(Form):
    name = StringField('Name')
    father_name = StringField('Father Name')
    # .... add fields you want

for field types - refer to this 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)

Once you have form then you can easily edit your content and directly save it to database

@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)

Upvotes: 1

Related Questions