iterate sqlalchemy query in my template python-flask

Issue

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.

Solution

all these functions are working fine, the problem was that I have space between tags, and I solved it by removing space in each tag.
this is the new function with strip() function :

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

Answered By – Abdellah ALAOUI ISMAILI

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