Home > other >  How to send information from a button in an html file to the routes.py file in flask to delete Datab
How to send information from a button in an html file to the routes.py file in flask to delete Datab

Time:12-22

I have the following problem: On my page there is a list in which you can enter things. These are then stored in the database. Now I also want that these can be deleted by clicking on the delete icon from the database, see picture.

List on Website

I have the following files:

routes.py

from page import app
from flask import render_template, redirect, url_for,request,flash
from page.models import Shoppinglist
from page.forms import Form_Shoppinglist
from page import db

@app.route('/shoppinglist', methods=['GET','POST'])
def Shoppinglist_Page():
    form = Form_Shoppinglist()
    
    if form.validate_on_submit():
        add_item =  Shoppinglist(product_name=form.product_name.data,
                                 category=form.category.data,
                                 purchased_quantity=form.purchased_quantity.data) 
        db.session.add(add_item)
        db.session.commit()
        return redirect(url_for('Shoppinglist_Page'))
    
    #delete
    #item = Shoppinglist.query.filter_by(id=????).first()
    #db.session.delete(item)
    #db.session.commit()
    
    if form.errors != {}: #If there are not errors from the validations
        for err_msg in form.errors.values():
            flash(f'Fehler bei der Eingabe: {err_msg}', category='danger')
    
    items = Shoppinglist.query.all()
    return render_template('shoppinglist.html',form=form,items=items)

shoppinglist.html

{% extends  'base.html'%}

{% block ActivShopping%}
    active
{% endblock %}

{% block css %}
    <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/style.css')}}"/>
{% endblock %}



{% block title %}
    Einkaufsliste
{% endblock %}

{% block content %}
    <div >
        <h1 >Einkaufsliste</h1>
        <!--Email Adresse-->
        <div >  
            <div >
                <div  style="height: 8vh;"> 
                  
                    <form  method="POST"  >
                        <!--Schutz gegen CSRF-->
                        {{ form.hidden_tag() }}
                        {{ form.product_name(, placeholder="Produktname") }}
                        {{ form.category(, placeholder="Kategorie") }} 
                        {{ form.purchased_quantity(, placeholder="Einkaufsmenge") }}
                        {{ form.submit_shoppinglist(, style="background-color: #6941c6; border:none; color: #f9f5ff; margin-left: 1vw;" )}}
                    </form>
                    
                </div>
                <hr >
            </div>  
        </div>

        <div >  
            <div >
                <table >
                    <thead>
                        <tr>
                            
                            <th scope="col">ID</th>
                            <th scope="col">Produktname</th>
                            <th scope="col">Kategorie</th>
                            <th scope="col">Lagermenge</th>
                            <th scope="col">Einkaufsmenge</th>
                            <th scope="col"></th>
                        </tr>
                    </thead>
                    <tbody>
     
                        {% for item in items %}
                        <tr>
                            <td>{{ item.id }}</td>
                            <td>{{ item.product_name }}</td>
                            <td>{{ item.category }}</td>
                            <td>{{ item.inventory_quantity }}</td>
                            <td>{{ item.purchased_quantity }}</td>
                            <td>
                                <button >
                                    <i  ></i>
                                </button>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>  
            </div>  
        </div>    

    </div>
{% endblock %}


Problem: I now want that when the trashcan of a column is pressed, the id of this object is passed to the routes. So that I can then delete this object in routes based on the id. How can I now pass this id from the shoppinglist.html to routes.py to delete the object by id?

** --------Update to solve--------**

shoppinglistt.html

  <form action="/delete" method="post">
                                    <button  name="button_delete" type="submit" value="{{item.id}}">
                                        <i  ></i>
                                    </button>
                                </form>

routes.py

@app.route('/delete',methods=['POST'])
def Test():
    item_id = request.form['button_delete']
    item = Shoppinglist.query.filter_by(id=item_id).first()
    db.session.delete(item)
    db.session.commit()

    return redirect(url_for('Shoppinglist_Page'))

CodePudding user response:

You can write another function in routes.py e.g.

@app.route('/deleteitem', methods=['GET'])
    def DeleteItem():
        items_to_delete = request.args.get('items')
        delete_items_from_db(items_to_delete)

Then add a onClick button to your trashcan icon that makes the request with the item data! Hope this helps, and I have understood your problem correctly.

  • Related