iterate sqlalchemy query over for loop in my template python-flask

409 views
Skip to first unread message

Abdellah ALAOUI ISMAILI

unread,
Oct 20, 2022, 4:05:02 AM10/20/22
to sqlalchemy
Hello,
I call a function in my template that returns sqlalchemy query result, (color value from the name of the tag). this is the query function :

def get_tag_color(name): 
return db.session.query(Tag.tag_color).filter(Tag.tag_name == name).scalar()

and I call it in my template file:

{% if server.tags %}
{% for tag in server.tags.split(",") %} 
<strong><span style="background-color: {{ get_tag_color(tag) }};" class="label"> {{tag}} </span></strong>
 {% endfor %} {% endif %}

the problem is that I get just one result of the first tag. do you have any idea what I miss?

Thank you 

Simon King

unread,
Oct 21, 2022, 4:23:48 AM10/21/22
to sqlal...@googlegroups.com
I don't understand the question. Are you saying that only one tag is displayed? If so, that's not a problem with SQLAlchemy, it's a problem with your template logic.

If that's not what you mean, you need to give us more information. What is the value of "server.tags", and what is the output from the template?

Simon

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
http://www.sqlalchemy.org/
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/4f9d1bab-479d-47d5-89cf-c305026ec3d7n%40googlegroups.com.

Abdellah ALAOUI ISMAILI

unread,
Oct 22, 2022, 10:37:06 AM10/22/22
to sqlalchemy
wheel ... in my template, I get just the first tag color, returned from the function.

this is the result of my HTML file source code :

<td>
<strong><span style="background-color: #ff830f;" class="label"> LOG  </span></strong>
<strong><span style="background-color: None;" class="label">  EXADATA  </span></strong>
<strong><span style="background-color: None;" class="label">  DMZ_PRIVE  </span></strong>
 </td>

I hope it's clear, 
thank you .

Simon King

unread,
Oct 25, 2022, 5:52:17 AM10/25/22
to sqlal...@googlegroups.com
Turn on debug logs (add echo="debug" to your db connection string) to see the SQL you are executing and the results you are getting back:


Are you sure that the database actually contains colours for those tags?

Simon

Abdellah ALAOUI ISMAILI

unread,
Oct 25, 2022, 9:38:36 AM10/25/22
to sqlalchemy
yes, all tags have a color... 
Can you suggest me another way to do this, please?  
thank you .

Simon King

unread,
Oct 27, 2022, 4:41:55 AM10/27/22
to sqlal...@googlegroups.com
I can't see anything obviously wrong with your code. If you turn on debug logs, you'll be able to see the SQL that is being executed, and the rows that are coming back. Paste those logs here and maybe we'll be able to help more.

Simon

Reply all
Reply to author
Forward
0 new messages