0

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

/myapplication
    Procfile
    run.py
    requirements.txt
    /app
        __init__.py
        mechanize_boilerplate.py
        views.py
        /static
        /templates

Here is my init.py

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import os

app = Flask(__name__)
# DB Configuration
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('postgres://{link I got from heroku}')
db = SQLAlchemy(app)


from app import views

Here is a portion of my views.py

from app import app
import mechanize_boilerplate
import os
from flask import Flask, render_template, request
from app import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    email = db.Column(db.String(120), unique=True)

    def __init__(self, name, email):
        self.name = name
        self.email = email

    def __repr__(self):
        return '<Name %r>' % self.name

@app.route('/db')
def dbtest():
    try:
        user = User('John', '[email protected]')
        db.session.add(user)
        db.session.commit()
    except:
        print 'DB error'
    return 'done'

Basically when I visit myapp/db I want to create one record with id, name and email (john, [email protected]).

Any thoughts?

4
  • What specifically doesn't work? Commented Sep 7, 2014 at 5:26
  • Nothing happens, it just prints 'DB error' which means the try failed. If I remove the 'try:' I get a 500 error. Commented Sep 7, 2014 at 5:29
  • What is the actual exception? Commented Sep 7, 2014 at 5:34
  • I'm not sure, how should I print the exception? Thanks! Commented Sep 7, 2014 at 5:49

3 Answers 3

3

It should be os.environ.get('DATABASE_URL')

Sign up to request clarification or add additional context in comments.

Comments

2

replace this to log error message:

@app.route('/db')
def dbtest():
try:
    user = User('John', '[email protected]')
    db.session.add(user)
    db.session.commit()
except Exception as e:
    f = open('/tmp/error.log', 'w')
    f.write(e.message)
    f.close()
return 'done'

post your error message.

2 Comments

Thanks! I printed e.message and got: 'NoneType' object has no attribute 'drivername'
check your configuration with configured Flask-SQLAlchemy
0

Another solution not mentionned is to use dj_database_url

import dj_database_url

DATABASES['default'] =  dj_database_url.config()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.