-1

So I got a delete route to delete data from SQL. How do I get the id from HTML so I can Delete the data from application.py using db.execute("DELETE FROM birthdays WHERE name=?",id)

Delete method reference:https://www.digitalocean.com/community/tutorials/how-to-modify-items-in-a-one-to-many-database-relationships-with-flask-and-sqlite

This is my code:

application.py:

import os

from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session

# Configure application
app = Flask(__name__)

# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True

# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///birthdays.db")

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":

        # TODO: Add the user's entry into the database
        name = request.form.get("name")
        month = request.form.get("month")
        date = request.form.get("date")
        if not name or month or date :
            return render_template("error.html", message="Missing something")
        db.execute("INSERT INTO birthdays (name,month,day) VALUES(?,?,?)",name,month,date)



        return redirect("/")

    else:

        # TODO: Display the entries in the database on index.html
        entries=db.execute("SELECT * FROM birthdays" )

        return render_template("index.html",entries=entries)


#FIX HERE
@app.route("/delete", methods=["GET", "POST"])
def delete(id):
    if request.method == "post":

        
        db.execute("DELETE FROM birthdays WHERE name=?",id)


        return redirect("/")

index.html:

<!DOCTYPE html>

<html lang="en">
    <head>
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap" rel="stylesheet">
        <link href="/static/styles.css" rel="stylesheet">
        <title>Birthdays</title>
    </head>
    <body>
        <div class="jumbotron">
            <h1>Birthdays</h1>
        </div>
        <div class="container">
            <div class="section">

                <h2>Add a Birthday</h2>
                <!-- TODO: Create a form for users to submit a name, a month, and a day -->
                <form action="/" method="post">
                    <input autocomplete="off" type="text" name="name" placeholder="Name">
                    <input autocomplete="off" type="number"min=1 max=12 name="month" placeholder="month">
                    <input autocomplete="off" type="number"min=1 max=31 name="date" placeholder="date">
                    <input type="submit" value="submit">


                </form>
            </div>

            <div class="section">

                <h2>All Birthdays</h2>
                <table>
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Birthday</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- TODO: Loop through the database entries to display them in this table -->
                        {% for person in entries%}
                            <tr>
                                <td>{{person.name}}</td>
                                <td>{{person.day}}/{{person.month}}</td>
                                <!--DELETE BUTTON-->
                                <td><form action="{{ url_for('delete', id=person.name) }}" method='post'><input  type="submit" value="delete"></form></td>
                            </tr>

                        {% endfor%}
                    </tbody>
                </table>
            </div>
        </div>
    </body>
</html>

1 Answers1

1

For the delete route, you need to add the id variable:

#FIX HERE
@app.route('/<int:id>/delete/', methods=('POST',))
def delete(id):

https://flask.palletsprojects.com/en/1.1.x/quickstart/#variable-rules

JBLaf
  • 776
  • 5
  • 10