0

problem is when i run the application creating csv out of form.html and later try to represent that in a table in sheet.html after opening the list i made out of reading csv file , and try to access each each column of a field of csv file i get error

{% for name,liferating,sources,reasons in answer %} ValueError: too many values to unpack (expected 4)

i think the problem is with textarea not been able to turn into csv column . and could you also tell me how to save all the option in checkbox into one column(sources of joy option in my form)

application.py

 import cs50
import csv

from flask import Flask, jsonify, redirect, render_template, request

# Configure application
app = Flask(__name__)

# Reload templates when they are changed
app.config["TEMPLATES_AUTO_RELOAD"] = True


@app.after_request
def after_request(response):
    """Disable caching"""
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response


@app.route("/", methods=["GET"])
def get_index():
    return redirect("/form")


@app.route("/form", methods=["GET"])
def get_form():
    return render_template("form.html")


@app.route("/form", methods=["POST"])
def post_form():
    if not request.form.get("name") or  not request.form.get("life-rating") or not request.form.get("reasons"):
        return render_template("error.html", message="please fill in the required fields")
    with open("survey.csv","a") as file:
        writer = csv.writer(file)
        writer.writerow((request.form.get("name") ,  request.form.get("life-rating") ,request.form.get("sources") ,request.form.get("reasons")))
    return redirect("/sheet")

@app.route("/sheet", methods=["GET"])
def get_sheet():
    with open("survey.csv","r") as file:
        reader = csv.reader(file)
        answers = list(reader)
    return render_template("sheet.html", answers=answers)

sheet.html

{% extends "layout.html" %}

{% block main %}
<h1>THE LIFE STUDY</h1>
<table>
    <tr>
        <th>NAME</th>
        <th>RATING YOUR LIFE</th>
        <th>SOURCES OF JOY </th>
        <th>REASONS , PLANS</th>
    </tr>
    {% for answer in answers %}
    <tr>
        <!-- this is where problem happens -->
        {% for name,liferating,sources,reasons in answer %}
        <td>{{name}}</td>
        <td>{{liferating}}</td>
        <td>{{sources}}</td>
        <td>{{reasons}}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>
{% endblock %}

form.html

{% extends "layout.html" %}

{% block main %}

    <!-- http://getbootstrap.com/docs/4.1/content/typography/ -->
    <h1 class="mb-3">Form</h1>

    <!-- http://getbootstrap.com/docs/4.1/components/forms/ -->
    <form action="/form" method="post">

        <!-- creating space for name and its label -->
        <div class = "form-group">
        <label for="Name">Your name </label>
        <input type="text" class="form-control" id="Name" name="name" placeholder="Your Name" >
        </div>

        <!-- creating a select option for life-rating -->
        <div class = "form-group">
        <label for="life-rating">How is your life going(rate it)</label>
        <select class="form-control" id="life-rating" name="life-rating" >
            <option value="" selected disabled>Give the rating from 1 to 5</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        </div>

         <!-- creating checkboxes for knowing what gives you pleasure -->
        <label for="life-enjoy">What is your source of joy these days</label>
        <div class="form-check" id="life-enjoy" >
            <label  class="form-check-label">
            <input class="form-check-input" type="checkbox" value="food" id="food" name="sources" >
            food</label>
            <br><label  class="form-check-label">
            <input class="form-check-input" type="checkbox" value="sports" id="sports" name="sources" >
            sports</label>
            <br><label  class="form-check-label">
            <input class="form-check-input" type="checkbox" value="practical projects" id="study and doing practical projects" name="sources" >
            study and doing practical projects</label>
            <br><label  class="form-check-label">
            <input class="form-check-input" type="checkbox" value="relationship" id="relationship" name="sources" >
            relationship</label>
        </div>

        <!-- creating textarea for knowing what are ur ambitions , and what are u gonna do about it -->
        <br><br>
        <textarea class="form-control" name="reasons" rows="15" columns="25" >
        write the reasons why you think you are leading your rated life and what
        things make it awesome or sucking and what u r gonna do about it
        </textarea>

        <br>
        <!-- http://getbootstrap.com/docs/4.1/components/buttons/ -->
        <button class="btn btn-primary" type="submit">Submit</button>

    </form>
1

Your answers is going to be a list of each list of answers (ie, a list of rows). The actual 'title' of those answers isn't in the file.

You only need to iterate over each line and then iterate over each item (in the comma separated list).

Let's say that someone filled out your form and chose "me, 1, sports, my reasons" for the 4 questions. The next person chose "you, 3, food, not sure". Your answers variable will be

[['me', '1', 'sports', 'my reasons'], ['you', '3', 'food', 'not sure']]

Your jinja template would then loop over answers as you've done

{% for answer in answers %}

and then, simply loop over the items in each list.

{% for item in answer %}
    <td>{% item %}</td>

that inner loop will run 4 times, and will line up with the 4 <tr> rows you already printed.

Can you see how that works?

1
  • Thanks it totally worked , actually i had thought that answer would be a tuple in answers list . Could you also tell me how to save multiple checkboxes in one column (sources of joy column) – Amber Aug 25 '19 at 5:17

You must log in to answer this question.

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