This is a question referring to lab 9 in CS50X. I didn't get an answer on Ed Discussion. In my code, I have a database with its elements. I'm trying to print out each row of that database in a tabular for in an HTML file using Jinja syntax. I have double checked and made sure that the elements of the database exist by printing them out on Python. Nothing seems to be happening on my webpage, though. So I simply have to print it out. I'm using Jinja syntax and Flask, and I'm new to it, which could explain any rookie mistakes. Here's my flask code:
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
bname = request.form.get("name")
bday = request.form.get("day")
bmonth = request.form.get("month")
db.execute("INSERT INTO birthdays (name, month, day) VALUES (?, ?, ?)", bname, bmonth, bday)
return redirect("/")
else:
tableau = db.execute("SELECT * FROM birthdays")
return render_template("index.html")
Here's my HTML
<div class="section">
<h2>All Birthdays</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
{% for row in tableau %}
<tr>
<td>{{row['name']}}</td>
<td>{{row['month']}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
I'm basically just trying to print each row of my database into each row of a table on HTML using Jinja syntax. My database has all the elements needed. I'm new to Flask and Jinja, and it would explain any rookie mistake.
Look at the documentation for render_template
.
It takes two arguments:
- template_name_or_list (Union[str, List[str]]) – the name of the template to be rendered, or an iterable with template names the first one existing will be rendered
- context (Any) – the variables that should be available in the context of the template.
But you are only passing it one:
render_template("index.html")
Since you haven’t passed it a context
, there are no variables available inside it, so tableau
does not exist.