user1903663
user1903663

Reputation: 1725

Using Python, flask and SQLAlchemy How can I print on an HTML page the result of a DB query?

I have a very simple query (just starting out with SQLAlchemy) and I know that it is working because it prints in the terminal but not on the HTML page, which is the problem.

As follows:

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import *
from models import db, Candidate, User, Template
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

some_engine = create_engine('databse_url')
Session = sessionmaker(bind=some_engine)
session = Session()

@application.route("/", methods=['GET', 'POST'])
def index():
    res = session.query(Template).all()
    for temp in res:
            print temp.spark_id
    return render_template('index.html')

In the terminal the 2 "spark_id" are printed but on the HTML page I just get nothing.

In the index.html page I have:

% if res %}
    {% for temp in res %}

        <p>{{ temp.spark_id }}</p>
        <p>{{ temp.created_at }}</p>

    {% endfor %}
{% endif %}

What is going wrong?

Upvotes: 3

Views: 3618

Answers (1)

dpoggi
dpoggi

Reputation: 82

You're not passing the query results to your template:

return render_template('index.html', res=res)

Upvotes: 6

Related Questions