0

I am using flask for the first time and jinja code is being displayed on the webpage. this is my html file:

    <!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>
                     <form>
                <input type="text" autocomplete="off" spellcheck="false" placeholder="Name" name="name">
                <input type="number" placeholder="Month" max="12" min="1" id="month">
                <input type="number" placeholder="Day" min="1" max="31" id="day">
                <input type="submit" value="Add Birthday">
            </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 -->
                        <tr><th>{{ name }}</th></tr>

                     </tbody>
                 </table>
             </div>
         </div>
     </body>
 </html>

it looks like that the browser is not understanding jinja even though i am using flask. this is 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").value
    return render_template("index.html" ,name=name)
    return redirect("/")

else:

    # TODO: Display the entries in the database on index.html

    return render_template("index.html")
1
  • Please edit the question and include how name is created in application.py. Sep 20 at 22:43
0

This name = request.form.get("name").value is going to throw an Attribute Error, which indicates the POST branch doesn't run. (the get returns a string, and a python string does not have a value attribute.)

A POST request is never submitted; <form> element needs a method attribute.

You must log in to answer this question.

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