Search code examples
pythonhtmlflasksqlalchemytags

iterate sqlalchemy query in my template python-flask


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 %}