问题描述
我遇到了这个错误:
我是新来的bluprint,并有导入数据库到mydatabase.py文件,这是模型文件的问题。 / b>
$ b
我的项目树
nikoofar /
run.py
书架/
__init__.py
mydatabase.py
main /
controllers.py
__init__.py
run.py
from bookshelf import app
if __name__ =='__main__':
app.run(debug = True,port = 8000)
bookshelf / intit .py
from flask import从bookhelf.main中获取Flask
。控制器从flask_sqlalchemy导入主
从mydatabase导入SQLAlchemy
导入pmenu
$ b app =烧瓶(__ name__,instance_relative_config = True)
db = SQLAlchemy(app)
db.init_app(app)
application.config ['SQLALCHEMY_DATABASE_URI'] ='mysql + pymysql://用户名:密码@ localhost / databasename'
app.config.from_object('config')
app.register_blueprint(main,url_prefix ='/')
bookshelf / main / controllers.py
from flask import蓝图
from bookshelf.mydatabase import *
from flask_sqlalchemy import SQLAlchemy
$ b main = Blueprint('main',__name__)
@main ()/ $)
def index():
g = pmenu.query.all()
print(g)
returnok
从书架import db 返回,如果删除,错误将改为:
$ b
bookshelf / mydatabase.py
db
class pmenu(db.Model):
__tablename__ ='p_menu'
id = db.Column(db.Integer,primary_key = True)
txt = db.Column(db.String(80),unique = True)
link = db.Column(db.String(1024))
def __init __(self,txt,link):
self.txt = txt
self.link = link
def __repr __(self):
return{'txt':+ self.txt +,'link':+ self .link +}
任何解决方案?
这实际上是一个简单但令人沮丧的问题。问题是你正在导入 BEFORE
你正在 __ init__中创建
db
的实例.py
如果将导入移动到 db = SQLAlchemy(app)
,它将工作:
from flask import Flask
$ b $ from flask_sqlalchemy import SQLAlchemy
app = Flask(__ name__)
app.config ['SQLALCHEMY_DATABASE_URI'] ='mysql + pymysql:// uername:password @ localhost / test'
db = SQLAlchemy(app )
from bookshelf.main.controllers import main#< - 在此移动
app.register_blueprint(main,url_prefix ='/')
I'm new in bluprint, and have problem with importing db into mydatabase.py file which is models file.
I've faced with this error:
The tree of my project
nikoofar/
run.py
bookshelf/
__init__.py
mydatabase.py
main/
controllers.py
__init__.py
run.py
from bookshelf import app
if __name__ == '__main__':
app.run(debug=True, port=8000)
bookshelf / intit.py
from flask import Flask
from bookshelf.main.controllers import main
from flask_sqlalchemy import SQLAlchemy
from mydatabase import pmenu
app = Flask(__name__, instance_relative_config=True)
db = SQLAlchemy(app)
db.init_app(app)
application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@localhost/databasename'
app.config.from_object('config')
app.register_blueprint(main, url_prefix='/')
bookshelf / main / controllers.py
from flask import Blueprint
from bookshelf.mydatabase import *
from flask_sqlalchemy import SQLAlchemy
main = Blueprint('main', __name__)
@main.route('/')
def index():
g = pmenu.query.all()
print (g)
return "ok"
The problem backs to from bookshelf import db
, and if I delete that, the error will be changed to:
bookshelf / mydatabase.py
from bookshelf import db
class pmenu(db.Model):
__tablename__ = 'p_menu'
id = db.Column(db.Integer, primary_key=True)
txt = db.Column(db.String(80), unique=True)
link = db.Column(db.String(1024))
def __init__(self, txt, link):
self.txt = txt
self.link = link
def __repr__(self):
return "{'txt': " + self.txt + ", 'link':" + self.link + "}"
Any solution?
This is actually a simple, yet frustrating issue. The problem is you are importing main BEFORE
you are creating the instance of db
in your __init__.py
If move the import to after your db = SQLAlchemy(app)
, it will work:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://uername:password@localhost/test'
db = SQLAlchemy(app)
from bookshelf.main.controllers import main #<--move this here
app.register_blueprint(main, url_prefix='/')
这篇关于烧瓶 - 蓝图 - sqlalchemy - 不能将名称“db”导入到moles文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!