title: Heroku Postgres Database with Flask and Python
body: <p>Im writing a dummy website for class and I'm having trouble connecting my Heroku Database to my app that's local for now until I push to Heroku. </p>
  
  <p>I'm not sure what the proper way to do this, and I've searched many videos/forums and I can't seem to get a straight answer from them. Ill post some of my code below. In dbconnect.py where the insert the heroku database credentials, like the URI, host, etc?</p>
  
  <pre><code>#app.py
  from flask import Flask, render_template, redirect, url_for, request, session, flash
  from functools import wraps
  
  
  app = Flask(__name__)
  
  app.secret_key = "Gundam"
  
  # login required decorator
  def login_required(f):
      @wraps(f)
      def wrap(*args, **kwargs):
          if 'logged_in' in session:
              return f(*args, **kwargs)
          else:
              flash('You need to login first.')
              return redirect(url_for('login_page'))
      return wrap
  
  
  @app.route('/')    
  def homepage():
      return render_template("main.html")
  
  @app.route('/dashboard/')
  @login_required
  def dashboard():
      return render_template("dashboard.html")    
  
  @app.errorhandler(404)
  def page_not_found(e):
      return render_template("404.html")
  
  @app.route('/login/', methods=["GET", "POST"])    
  def login_page():    
      error = ''
      try:
          if request.method == "POST":
              attempted_username = request.form['username']
              attempted_password = request.form['password']
  
              if attempted_username == "admin" and attempted_password == "password":
                  session['logged_in'] = True
                  flash('You were just logged in!')
                  return redirect(url_for('dashboard'))
              else:
                  error = "Invalid Username or Password."    
          return render_template("login.html", error=error)        
      except Exception as e:    
          return render_template("login.html", error=error)
  
  @app.route('/logout/')
  def logout():
      session.pop("logged_in", None)        
      return redirect(url_for('homepage'))
  
  
  
  if __name__ == '__main__':
      app.run(debug = True)
  
  
  dbconnect.py
  
  import os
  import psycopg2
  import urlparse
  
  urlparse.uses_netloc.append("postgres")
  url = urlparse.urlparse(os.environ[""])
  
  conn = psycopg2.connect(
      database=url.path[1:],
      user=url.username,
      password=url.password,
      host=url.hostname,
      port=url.port
  )
  </code></pre>