I am trying to make a web app using Flask, but I am having trouble with the Jinja syntax. I am following CS50. I want to take the student's name and dorm from index.html and display it on success.html. However, I am unable to do this. All that displays is
"You have successfully registered! from has registered." Whereas I want it to say "You have successfully registered! [STUDENT] from [DORM NAME] has registered."
Here is the main.py code
Here is the index.html code
And here is the success.html code
My guess is that the problem is in the success.html code, but I cannot figure out what I am doing wrong and why I cannot grab the input variables from the html inputs. Any help would be much appreciated. Thank you!
"You have successfully registered! from has registered." Whereas I want it to say "You have successfully registered! [STUDENT] from [DORM NAME] has registered."
Here is the main.py code
Code:
from flask import Flask, render_template, request
import csv
app = Flask(__name__)
students = []
@app.route('/')
def index():
# does this go here?
return render_template('index.html')
@app.route('/register', methods=["POST", "GET"])
def register():
name = request.form.get("name")
if not request.form.get("name") or not request.form.get("dorm"):
return "Failure!"
file = open("registered.csv", "a")
writer = csv.writer(file)
writer.writerow((request.form.get("name"), request.form.get("dorm")))
file.close()
return render_template("success.html")
@app.route('/registered')
def registered():
file = open("registered.csv", "r")
reader = csv.reader(file)
students = list(reader)
return render_template("registered.html", students=students)
@app.route("/registrants")
def registrants():
file = open("registered.csv", "r")
reader = csv.reader(file)
students = list(reader)
return render_template("registered.html", students=students)
Here is the index.html code
Code:
{% extends "layout.html" %}
{% block body %}
<h1>register</h1>
<form action="/register" method="post">
<input name="name" placeholder="Name" type="text" id="name">
<select name="dorm" method="post">
<option disabled selected value="" id="dorm" name="dorm">Dorm</option>
<option value="apley court">apley court</option>
<option value="candy">candy</option>
<option value="d dorm">d dorm</option>
</select>
<input type="submit" value="Register"><br>
<p>Or see who else is <a href="{{ url_for('registered') }}">REGISTERED</a></p>
</form>
{% endblock %}
And here is the success.html code
Code:
{% extends "layout.html" %}
{% block body %}
You have successfully registered!
{{ name }} from {{ dorm }} has registered.
{% endblock %}
My guess is that the problem is in the success.html code, but I cannot figure out what I am doing wrong and why I cannot grab the input variables from the html inputs. Any help would be much appreciated. Thank you!