vendredi 24 juillet 2015

No display of anything when running a sqlite query within a html page with flask framework

Losing my mind on this one. If anyone can give me some hints, it would be great

This is my views.py file

from flask import render_template
from app import app
import sqlite3
from flask import g

DATABASE = '/Users/admin/syncano/app/syncano.db'

def connect_db():
    return sqlite3.connect(DATABASE)

@app.before_request
def before_request():
    g.db = connect_db()

@app.after_request
def after_request(response):
    g.db.close()
    return response

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 col1,col2 from toto where col1 = 'grand test'")
    return render_template('show_results.html', entries = entries)    

This is my show_results.html file

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

Yet , nothing is appearing.

I've ran the sqlite and I have data in it

sqlite3 syncano.db

SELECT * FROM toto;

grand test|aimer test
grand1 test1|aimer1 test1

Can anyone tell me what I'm doing wrong, please?

Aucun commentaire:

Enregistrer un commentaire