Skip to content
Advertisement

Flask: Python List is displaying values in HTML file with quote marks and round brackets

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_name = list() #this is a list contstructor statement so have () brackets, usually when declaring list we use [] brackets
        for name in curr:
            list_name.append(name)
        return list_name
    else:
        return False

It is being called this way

entered_names = get_entered_datalist(session['v_firstname'], session['v_lastname'])

and passed to the html file this way

return render_template('monthlybudget.html', title='Enter Monthly Budget Data', form=form, entereddata=entered_names)

The display code in HTML file is as below

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

but it is being displayed with round brackets and quote marks as below display shows list with round brackets and quote marks

The query getting the values from database is as below

select concat([First Name], ' ', [Last Name] ) as name from [dbo].[EAMonthlyBudget]
            where [Report to First Name]= :firstname and [Report to Last Name] = :lastname
            and [budget month] = datename(month,getdate()) and [budget year] = year(getdate());

I added a print statement in my python code to check what gets returned by the function and that is as below (shows with the brackets and quote marks)

 [12/Aug/2022 15:29:44] "GET /static/images/nu_logo2.png HTTP/1.1" 304 -
[('Janak Shah',), ('Julie Wang',)]
-

How do I display the data without the brackets and quote marks?

Advertisement

Answer

Your curr variable contains tuples. try this code:

for name in curr:
    list_name.append(name[0])

I think you need to check of index out for range error.

I hope this helps.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement