I have a Python 2.7 / Flask app running on Heroku that scrapes data and I now want to store that information in a database.
I’ve tried to follow tutorials like this one and apply this to my case but I can’t get it to work. I have created & promoted my postgres database successfully on heroku.
I am fairly new to the Python project architecture and I suspect a simple problem in my setup.
Here is my project structure
JavaScript
x
11
11
1
/myapplication
2
Procfile
3
run.py
4
requirements.txt
5
/app
6
__init__.py
7
mechanize_boilerplate.py
8
views.py
9
/static
10
/templates
11
Here is my init.py
JavaScript
1
12
12
1
from flask import Flask
2
from flask.ext.sqlalchemy import SQLAlchemy
3
import os
4
5
app = Flask(__name__)
6
# DB Configuration
7
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('postgres://{link I got from heroku}')
8
db = SQLAlchemy(app)
9
10
11
from app import views
12
Here is a portion of my views.py
JavaScript
1
28
28
1
from app import app
2
import mechanize_boilerplate
3
import os
4
from flask import Flask, render_template, request
5
from app import db
6
7
class User(db.Model):
8
id = db.Column(db.Integer, primary_key=True)
9
name = db.Column(db.String(80))
10
email = db.Column(db.String(120), unique=True)
11
12
def __init__(self, name, email):
13
self.name = name
14
self.email = email
15
16
def __repr__(self):
17
return '<Name %r>' % self.name
18
19
@app.route('/db')
20
def dbtest():
21
try:
22
user = User('John', 'Foo@bar.com')
23
db.session.add(user)
24
db.session.commit()
25
except:
26
print 'DB error'
27
return 'done'
28
Basically when I visit myapp/db I want to create one record with id, name and email (john, foo@bar.com).
Any thoughts?
Advertisement
Answer
It should be os.environ.get('DATABASE_URL')