Is there a way to delete row from mysql with flask. so that we can use javascript confirm or sweetalert

106 Views Asked by At

I tried my best to proceed according to this site, but to delete it, I used sweetalert to make sure it was deleted But it didn't happen The problem is that by clicking on the delete option, we must first get the user's id, then go to the delete page, but before deleting the line, use sweetalert. How?

@app.route('/pythonlogin/delete', methods =['GET'])
def delete():
    if 'loggedin' in session:
        deleteuser = request.args.get('userid')

        conn = mysql.connect()
        cur = conn.cursor(pymysql.cursors.DictCursor)
        cur.execute('DELETE FROM accounts WHERE userid= %s', (deleteuser, ))
        conn.commit()
    return redirect(url_for('User_management', userid=deleteuser))
<td><a id ="delete" href="{{url_for('User_management', userid=user.userid)}}"  class="btn btn-primary">delete</a></td>`
<script>
$('a#delete').click(function () {
    var url = $(this).attr('href');
    var arguments = url.split('?')[1].split('=');
    arguments.shift();
    Swal.fire({
        title: 'آیا مطمئن هستید؟',
        text: "کاربر پاک شده غیر قابل بازگشت است",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'بله پاک کن',
        cancelButtonText: 'انصراف'
    })
    
    .then((result) => {
        if (result.isConfirmed) {
        
            location ="{{url_for('delete',userid = arguments)}}"
        
        }
        else{location ="{{url_for('User_management',userid = arguments)}}"}
    })
});



</script>
1

There are 1 best solutions below

0
Sanaz On

I found the solution A third page called getid is required

main.python

@app.route('/pythonlogin/getid', methods =['GET'])
def getid():
    if 'loggedin' in session:
        deleteuser = request.args.get('userid')
        session['deleteuser'] = deleteuser
    return render_template("getid.html", deleteuser = deleteuser)
    

    
@app.route('/pythonlogin/delete', methods =['GET'])
def delete():
    if 'loggedin' in session:
        deleteuser = session['deleteuser']
        
        conn = mysql.connect()
        cur = conn.cursor(pymysql.cursors.DictCursor)
        cur.execute('DELETE FROM accounts WHERE userid= %s', (deleteuser, ))
        conn.commit()
    return redirect(url_for('User_management', userid=deleteuser))
    
    
    return render_template("delete.html", deleteuser = deleteuser)

User_managment.html

    <tbody>
          {% for user in users_list %}
          <tr>
            <td>{{user.userid}}</td>
            <td>{{user.username }}</td>
            <td><a href="{{url_for('view', userid=user.userid, partid = user.partid, levelid = user.levelid)}}" class="btn btn-success">نمایش</a></td>
            <td><a href="{{url_for('edit', userid=user.userid)}}" class="btn btn-primary">ویرایش</a></td>
            <td><a href="{{url_for('getid', userid=user.userid)}}"  class="btn btn-primary">حذف</a></td>

          </tr> 
        {% endfor %}


getid.html



<body onLoad="myFunction();">
<script>

function myFunction() {
 var url = window.location.href;

    var arguments = url.split('?')[1].split('=');
    arguments.shift();

    Swal.fire({
        title: 'آیا مطمئن هستید؟',
        text: "کاربر پاک شده غیر قابل بازگشت است",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'بله پاک کن',
        cancelButtonText: 'انصراف'
    }).then((result) => {
        if (result.isConfirmed) {
        
            location ="{{url_for('delete',userid = arguments)}}";
        
        }
        else{location ="{{url_for('User_management',userid = arguments)}}";};
    });
 
 
    
}





</script>