I'm deeply confused about how CSV files work in this context. Below is my application.py code and my form.html code; I can't see how to use Jinja to call on the information stored in my CSV file. In my mind, I visualise the CSV as a literal list of values separated by commas, and so I have no idea how to go about taking those values (after storing them in surveyReader) and putting the information inside the correct place inside the table I set up in the html file.
I've been debugging for almost a week now, and I really have never wanted to quit more. Whenever I run my program, the form works but the sheet/table doesn't display--it gives me a 500 internal server error. Please help!
relevant application.py code:
@app.route("/form", methods=["POST"])
def post_form():
name = request.form.get("name")
if not name:
render_template("error.html", message = "missing name!")
gender = request.form.get("gender")
if not gender:
render_template("error.html", message = "missing gender!")
tShirtSize = request.form.get("tShirtSize")
if not tShirtSize:
render_template("error.html", message = "missing t-shirt size!")
feedback = request.form.get("feedback")
# actually store on cs50 csv file
with open('survey.csv', 'a', newline='') as csvfile:
surveyWriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
surveyWriter.writerow((name, gender, tShirtSize, feedback))
csvfile.close()
return redirect("/sheet")
@app.route("/sheet", methods=["GET"])
def get_sheet():
# open a reader that reads the values of the csv file into
with open('survey.csv', newline='') as csvfile:
surveyReader = csv.reader(csvfile, delimiter=' ', quotechar='|')
return render_template("sheet.html", row = row)
sheet.html code:
{% extends "layout.html" %}
{% block main %}
<table class = "table table-dark">
<tr>
<th scope = "col">
Name
</th>
<th scope = "col">
Gender
</th>
<th scope = "col">
t-shirt size
</th>
<th scope = "col">
feedback
</th>
</tr>
<tbody>
{% for row in surveyReader %}
<tr>
<td>
{{ row[0] }}
</td>
<td>
{{ row[1] }}
</td>
<td>
{{ row[2] }}
</td>
<td>
{{ row[3] }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}