0

Can anyone please tell me why my "Remove Entry" from DB buttons don't work?

I included the python.app code below the 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><br>
                    <!-- form for user to enter birthday -->
                    <form action="/" method="post">
                        <input name="name" type="text" placeholder="Name" />
                        <input name="month" type="text" placeholder="Month" />
                        <input name="day" type="text" placeholder="Day" />
                        <input type="submit" placeholder="Add Birthday" />
                    </form>
                </div>
    
                <div class="section">
    
                    <h2>All Birthdays</h2>
                    <table>
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Birthday</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- jinja loop to display db values in table form -->
                            {% for person in persons %}
                            <tr>
                            <td>
                                <td>{{ person["name"] }}</td>
                                <td>{{ person["month"] }}/{{ person["day"] }}</td>
                                <td>
                                <form action="/" method="post">
                                    <input name="id" type="hidden" value="{{ [person.id] }}">
                                    <input type="submit" value="Remove Entry">
                                </form>
                                </td>
    
                            </tr>
    
                            {% endfor %}
    
                        </tbody>
                    </table>
                </div>
            </div>
        </body> </html>
    
    
   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=["POST"])
def remove_name():
    # Remove name from DB
    id = request.form.get("id")
    if id:
        db.execute("DELETE FROM birthdays WHERE id = ?", id)
    return redirect("/")
    



@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        # Add user's BD to DB

        # Python get via POST user's BD submission
        name = request.form.get("name")
        month = request.form.get("month")
        day = request.form.get("day")

        # Access sqlite to INSERT bd values into db
        db.execute("INSERT INTO birthdays (name, month, day) VALUES (?,?,?)", name, month, day)

        # After updating the user's BD in the DB, redirect user to updates table
        return redirect("/")

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

1 Answer 1

2

One problem is the html sends person.id as a list ( value="{{ [person.id] }}) so it will never find a match in the db.

Once that is fixed another problem will be discovered: it will not add records. There are two routes with the same "name". Change the name of the route for the remove routine (and the corresponding "action" attribute in the appropriate form).

1
  • Thank you so much :-) Commented Dec 24, 2021 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .