Click here to Skip to main content
13,739,613 members
Rate this:
Please Sign up or sign in to vote.
Im currently trying to get a register and login working with Flask using SQLAlchemy for the database side of things. When I run the file I get the error:

ImportError: cannot import name db in Flask

I'm fairly new to python flask and can't seem to figure this out. Thank you in advance for the suggestions.

What I have tried: File
from flask import Flask, request, render_template, redirect, url_for
from forms import SignupForm

from models import db
def init_db():
    db.init_app(app) = app

from flask_login import LoginManager, login_user, login_required, logout_user 
login_manager = LoginManager()

app = Flask(__name__)
app.secret_key = 'secretkeyhereplease'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////database/database.sqlite'

def index():
    return "Welcome to Flask"

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    form = SignupForm()

    if request.method == 'GET':
        return render_template('signup.html', form = form)
    elif request.method == 'POST':
        if form.validate_on_submit():
            if user.query.filter_by(
                return "Email address already exists"
                newuser = User(,


                return "Will create user here"
            return "Form didn't validate"

def load_user(email):
    return User.query.filter_by(email = email).first()

def protected():
    return "protected area"

app.route('/login', methods=['GET', 'POST'])
def login():
    form = SignupForm()

    if request.method == 'GET':
        return render_template('login.html', form=form)
    elif request.method == 'POST':
        if form.validate_on_submit():
            if user:
                if usee.password ==
                    return "User logged in"
                    return "Wrong password"
                return "User doesn't exist"
            return "Form not validated"     

if __name__ == '__main__':
    app.init_db()'', debug=True) File
from app import app
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)

class User(db.Model):
    email = db.Column(db.String(80), primary_key=True, unique=True)
    password = db.Column(db.String(80))

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

    def __repr__(self):
        return '<User %r>' %

    def is_authenticated(self):
        return True
    def is_active(self):
        return True
    def is_anonymous(self):
        return False
    def get_id(self):
        return str(
Posted 10-Nov-17 9:04am
Updated 14-Sep-18 4:56am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should import and initialize db from, then import db to models file

from flask_sqlalchemy import SQLAlchemy

Then initialize it
db = SQLAlchemy(app)

from app import db

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 14 Sep 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100