《Flask入门教程》第40页:输入>>> from app import db 报错:
from app import db
Traceback (most recent call last):
File “”, line 1, in
ImportError: cannot import name ‘db’
我前面是按照书上的要求补充了app.py
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template
import os, sys
app = Flask(name)
app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///’ + os.path.join(app.root_path, ‘data.db’)
app.config[‘SQLALCHEMY_TRACK_MODIFICATION’] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(20))
class Movie(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(60))
year = db.Column(db.String(4))
name = ‘Grey Li’
movies = [
{'title': 'My Neighbor Totoro', 'year': '1988'},
{'title': 'Dead Poets Society', 'year': '1989'},
{'title': 'A Perfect World', 'year': '1993'},
{'title': 'Leon', 'year': '1994'},
{'title': 'Mahjong', 'year': '1996'},
{'title': 'Swallowtail Butterfly', 'year': '1996'},
{'title': 'King of Comedy', 'year': '1999'},
{'title': 'Devils on the Doorstep', 'year': '1999'},
{'title': 'WALL-E', 'year': '2008'},
{'title': 'The Pork of Music', 'year': '2012'},
]
@app.route(’/’)
def index():
return render_template('index.html', name=name, movies=movies)