Python Flask: List object not iterating in HTML file

Issue

I have the below function which returns a list.

def get_entered_datalist(firstname, lastname):
    if firstname and lastname:
        curr = db.session.execute(entered_data, {'firstname': firstname, 'lastname': lastname})
        list_firstname = list()
        list_lastname = list()
        for firstname, lastname in curr:
            list_firstname.append(firstname)
            list_lastname.append(lastname)
        return {'v_firstname': list_firstname, 'v_lastname': list_lastname}
    else:
        return False

I call it in another function and store the values returned (and I assume that the variable used to store the returned list is automatically a list as I am assigning a list to it) and then pass it to the HTML page as shown below

def monthlybudget():
    form = MonthyBudget()
    reports that have already been entered for this month
    entered_names = get_entered_datalist(v_firstname, v_lastname) 
    #entered_names = ['Yogesh', 'Ramesh', 'Mahesh']
..... more code unrelated to this issue...... and then the last line is below
return render_template('monthlybudget.html', title='Enter Monthly Budget Data', form=form, entereddata=entered_names)

Then in the HTML page, I show it this way.

{% for x in entereddata %}
    <p>{{ x }} </p>
    {% endfor %}

The issue is that I get an error that ‘bool’ object is not iterable.

 File "C:\abc\NU\python_projects\trail\templates\monthlybudget.html", line 5, in block 'content'
    {% for x in entereddata %}
TypeError: 'bool' object is not iterable
127.0.0.1 - - [10/Aug/2022 16:07:25] "GET /monthlybudget HTTP/1.1" 500 -

My issue is, why is python Flask thinking that ‘entereddata’ is a boolean object, is it not a list? While testing, I commented the line where I am assigning the values returned from the function to ‘entered_names’ variable and directly typed 3 values (you can see that line as a comment in my second code block) and then it worked, how come it is not working when I am assiging the output of the function to ‘entered_names’ variable. Is my function which is supposed to return a list not returning anything (I tested the sql statement directly and it returns data in SSMS). How to check what the function is returning? How do I make this work? Please advise

Solution

So I think the problem is that when you call get_entered_datalist, and when no data is found, it returns the boolean value False.

def get_entered_datalist(firstname, lastname):
    if firstname and lastname:
        curr = db.session.execute(entered_data, {'firstname': firstname, 'lastname': lastname})
        list_firstname = list()
        list_lastname = list()
        for firstname, lastname in curr:
            list_firstname.append(firstname)
            list_lastname.append(lastname)
        return {'v_firstname': list_firstname, 'v_lastname': list_lastname}
    else:
        return False

One simple way to work around it is to add a check inside your Jinja2 template.

{% if entereddata %}
    {% for x in entereddata %}
        <p>{{ x }} </p>
    {% endfor %}
{% else %}
    <p>No data found</p>
{% endif %}

Answered By – Ashwin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published