Python Flask How to Delete Multiple records using checkbox with getlist and pymysql
#app.py from flask import Flask, render_template, request from flaskext.mysql import MySQL import pymysql app = Flask(__name__) app.secret_key = "Cairocoders-Ednalan" mysql = MySQL() # MySQL configurations app.config['MYSQL_DATABASE_USER'] = 'root' app.config['MYSQL_DATABASE_PASSWORD'] = '' app.config['MYSQL_DATABASE_DB'] = 'testingdb' app.config['MYSQL_DATABASE_HOST'] = 'localhost' mysql.init_app(app) @app.route('/', methods=['GET', 'POST']) def index(): conn = mysql.connect() cur = conn.cursor(pymysql.cursors.DictCursor) msg = '' if request.method == 'POST': #test = request.form.getlist('mycheckbox') for getid in request.form.getlist('mycheckbox'): print(getid) cur.execute('DELETE FROM employee WHERE id = {0}'.format(getid)) conn.commit() msg = 'Successfully Deleted' return render_template('index.html', msg=msg) return render_template('index.html')
//templates/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Python Flask How to Delete Multiple records using checkbox with getlist and pymysql</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <p><h2>Python Flask How to Delete Multiple records using checkbox with getlist and pymysql</h2></p> <div class="table-responsive"> <form method="POST" action="/"> <table id="mytable" class="table table-bordred table-striped"> <thead> <th><input type="checkbox" id="checkall" /></th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th>Email</th> <th>Contact</th> <th>Edit</th> <th>Delete</th> </thead> <tbody> <tr> <td><input type="checkbox" value="21" name="mycheckbox" class="checkthis" /></td> <td>Airi</td> <td>Satou</td> <td>New Cabalan Olongapo City, Philippines</td> <td>cairocoders@gmail.com</td> <td>+923335586757</td> <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button></p></td> <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td> </tr> <tr> <td><input type="checkbox" value="22" name="mycheckbox" class="checkthis" /></td> <td>Angelica</td> <td>Ramos</td> <td>New Cabalan Olongapo City, Philippines</td> <td>cairocoders0711@gmail.com</td> <td>+923335586757</td> <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button></p></td> <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td> </tr> <tr> <td><input type="checkbox" value="23" name="mycheckbox" class="checkthis" /></td> <td>Ashton</td> <td>Cox</td> <td>New Cabalan Olongapo City, Philippines</td> <td>cairocoders0711@gmail.com</td> <td>+923335586757</td> <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button></p></td> <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td> </tr> </tbody> </table><input type="submit" value="Delete All Selected" class="btn btn-primary"> <br/> {% if msg %} <div class="alert alert-danger" role="alert">{{ msg }}</div> {% endif %} </form> </div> </div> </div> </div> <script> $(document).ready(function(){ $("#mytable #checkall").click(function () { if ($("#mytable #checkall").is(':checked')) { $("#mytable input[type=checkbox]").each(function () { $(this).prop("checked", true); }); } else { $("#mytable input[type=checkbox]").each(function () { $(this).prop("checked", false); }); } }); $("[data-toggle=tooltip]").tooltip(); }); </script> </body> </html>