Forums

Object error in HTML from Flask

Morning I have an error on my HTML template page when viewed in a browser. I am trying to show an SQL query result as an object rather than a list or table. It is a simple 'count' query. But the error I am getting says...

[<flask_app.Comment object at 0x7f0de640b128>]

In flask fie I have... + + + + + def league(): total = Comment.query.order_by(Comment.id.desc()).limit(1).all() return render_template("def.html", total=total) ++++++ Comment is the class name

In my template I have... +++++++++ {{ total }} ++++++++ This is in the body of the HTML.

Sorry if format of question is wrong. Any ideas guys???

Thank you in advance.

M

That query is not a count query. It's just a query that returns a single Comment object, which is what is being shown in your page (a list with one Comment object in it). Check the documentation for your ORM to find out how to do a count query.

Hi Glenn So the error is in the SQL in Flask file NOT in the HTML template file?? M

It's in this query: Comment.query.order_by(Comment.id.desc()).limit(1).all() - that selects the first Comment when they are ordered by id and that is exactly what is being shown by your template.

Hi Oh man....I thought I had replaced it with a count query. Really sorry... Thanks though M

Glad we could help you sort it out :-)

Hi Giles/Glenn I am really struggling to get an equivalent query to SELECT COUNT(*) FROM comments; to work in my flask app. It’s works in bash console. I know it got to be different in flask. I just have tried all sorts as the other sql query for my main page works. Any chance of a pointer please?? Many thanks M

There's no reason that the console and flask should be different if you're using the same ORM. Check the documentation for the ORM that you're using to see how to write a count query.