Issue
I want to render results from a SQLite database query in my template. However, they all look like {ColIntitule': u'I like IceCream'}
. I don't want the {}
or column name. How do I render this correctly?
def query_db(query, args=(), one=False):
cur = g.db.execute(query, args)
rv = [dict((cur.description[idx][0], value) for idx, value in enumerate(row)) for row in cur.fetchall()]
return (rv[0] if rv else None) if one else rv
@app.route('/toto')
def toto():
entries = query_db("select ColIntitule from toto where col1 = 1")
return render_template('show_results.html', entries = entries)
show_results.html
:
{% extends "layout.html" %}
{% block body %}
<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry }}</h2>
<br>
{% else %}
<li><em>No entry here</em>
{% endfor %}
</ul>
{% endblock %}
Solution
entries
is a list of dict
objects so when you print them in your template using {{ entry }}
, you are printing the dict
repr.
Your template should look more like this
{% extends "layout.html" %}
{% block body %}
<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry["ColIntitule"] }}</h2>
<br>
{% else %}
<li><em>No entry here</em>
{% endfor %}
</ul>
{% endblock %}
Answered By - Josh J
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.