0

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 %}
1
  • If there's a 500, you likely have an error message and stack trace on your terminal. That error message might point you to the mistake.
    – Blauelf
    Sep 2 '19 at 13:43
0

i think what's wrong is that you supplied the render template statement with row while you should have supplied it with surveyreader , row is just an iterator and need not be supplied to render template so your statement be like this $return render_template("sheet.html",surveyreader=surveyreader)

You must log in to answer this question.

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