我是python和flask的新手并且正在使用Flask Mega-Tutorial,但是:我很难让flask shell
识别我的自定义符号/命令。
当我尝试通过在我的虚拟环境中键入User
来访问模型flask shell
作为符号时,我得到NameError: name 'User' is not defined
。
User
应返回:<class 'application.models.User'>
,但会显示错误。
我不明白的是app
符号似乎工作正常,并按原样返回<Flask 'application'>
。
我在这里做错了什么?有进口的东西吗?
我做了一些研究:this看起来像我的问题,但没有使用app.sell_context_processor
装饰器。
我还尝试了我的代码的变体:将导入名称从应用程序更改为应用程序,因为我从教程中的默认值更改了这些名称并使用user
而不是User
(低于大写)但似乎没什么用。
请帮我解决这个问题!
错误
(venv) MBP:books mbpp$ flask shell
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
App: application
Instance: /Users/mbpp/Sites/books/instance
>>> app
<Flask 'application'>
>>> User
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'User' is not defined
来自 application.py 的我的代码
from application import app, db
from application.models import User, Book, State, Course, BookTitle, Author
@app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'State': State, 'BookTitle': BookTitle, 'Author': Author}
和 __ init __。py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
# initiate the Flask app
app = Flask(__name__)
# use the config.py file for configuration
app.config.from_object(Config)
# use SQLAlchemy for database management
db = SQLAlchemy(app)
# use Flask-Migrate extension for database migration management
migrate = Migrate(app, db)
# use Flask-Login extension for login form
login = LoginManager(app)
login.login_view = 'login'
from application import routes, models
以及 models.py (我建立了一个用户可以销售图书的网站)
from application import db, login
from datetime import datetime
from werkzeug.security import generate_password_hash,
check_password_hash
from flask_login import UserMixin
# create a table to store users
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(64), index = True, unique = True)
email = db.Column(db.String(120), index = True, unique = True)
password_hash = db.Column(db.String(128))
phone = db.Column(db.String(64))
books = db.relationship('Book', backref='seller_name', lazy='dynamic')
def __repr__(self):
return '<User: {}>'.format(self.username)
# create a password hash
def set_password(self, password):
self.password_hash = generate_password_hash(password)
# check the password hash against a user given password
def check_password(self, password):
return check_password_hash(self.password_hash, password)
# create a table to store information on a book for sale
class Book(db.Model):
id = db.Column(db.Integer, primary_key = True)
course_id = db.Column(db.Integer, db.ForeignKey('course.id'))
title = db.Column(db.Integer, db.ForeignKey('booktitle.id'))
author = db.Column(db.Integer, db.ForeignKey('author.id'))
price = db.Column(db.Integer)
isbn = db.Column(db.String(64), index = True)
state_id = db.Column(db.Integer, db.ForeignKey('state.id'))
state_description = db.Column(db.String(256))
seller_id = db.Column(db.Integer, db.ForeignKey('user.id'))
creation_timestamp = db.Column(db.DateTime, index = True, default = datetime.utcnow)
def __repr__(self):
return '<Book: {}>'.format(self.title)
# create a table to store different states books can be in
class State(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(128), index = True)
books = db.relationship('Book', backref='state', lazy='dynamic')
def __repr__(self):
return '<State: {}>'.format(self.name)
# create a table to store courses
class Course(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(128), index = True)
year = db.Column(db.Integer, index = True)
books = db.relationship('Book', backref='course', lazy='dynamic')
def __repr__(self):
return '<Course: {}>'.format(self.name)
# create a table to store booktitles
class BookTitle(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(128), index = True)
books = db.relationship('Book', backref='book_title', lazy='dynamic')
def __repr__(self):
return '<Book title: {}>'.format(self.title)
# create a table to store authors
class Author(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(128), index = True)
books = db.relationship('Book', backref='author_name', lazy='dynamic')
def __repr__(self):
return '<Author: {}>'.format(self.name)
# user loader for Flask-Login extension, gets users ID
@login.user_loader
def load_user(id):
return User.query.get(int(id))
答案 0 :(得分:8)
非常感谢Miguel,FLASK Mega Tutorial(go check that out)的作者解决了我的问题!
正如他在a comment below my question中指出的那样:你不能拥有一个同名的模块和包。所以没有应用程序文件夹和application.py在同一时间。
我将'application.py更改为'theapp.py',现在flask shell
就像一个魅力!除了在终端中运行export FLASK_APP=theapp.py
之外,我不需要更改文件中的任何内容。
答案 1 :(得分:8)
通过设置
,需要告知Flask如何导入它FLASK_APP:(venv) $ export FLASK_APP=microblog.py
如果您使用的是Microsoft Windows,请在上面的命令中使用set
代替export
。