I want to connect to a pre-existing postgres database which has no models associated with it in my app. Unsurprisingly perhaps, this is proving troublesome added to which this is my first attempt with Python and Flask.
The app/py code is:
import os
from flask import Flask
from flask import render_template
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://blah:[email protected]:5432/mydb'
db = SQLAlchemy(app)
db.create_all()
db.session.commit()
@app.route("/")
def main():
return render_template('index.html')
@app.route('/base')
def base():
myusers = users.all()
return render_template('base.html')
if __name__ == '__main__':
app.run(debug=True)
the def main() works so that much I have achieved.
In the view (base.html) we have:
{% for user in myusers %}
<div><p>{{ user.first_name }} </b></p></div>
{% endfor %}
When I go to the base.html page the persistent error is NameError: global name 'users' is not defined
What, of many things probably, am I doing wrong here? All help appreciated. Thank you.