蓝图,PyMongo在烧瓶 [英] Blueprints, PyMongo in Flask

查看:297
本文介绍了蓝图,PyMongo在烧瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的父母 login.py

  app.config.from_object('config')
from flask.ext。 pymongo导入PyMongo
从子导入子
从child2导入child2

$ b app = Flask(__ name__)
app.register_blueprint(child2.child2)
app.register_blueprint(child.child)

在我的子元素中。 py

  from app import app 
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child = Blueprint('child',__name__)

child2.py 与child具有相同的结构:

  from app import app 
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child2 =蓝图('child2',__name__)

以下是错误讯息:

  raise异常('duplicate config_prefix'%s''%config_prefix)
异常:重复的config_prefixMONGO

我在蓝图中试过以下内容:

$ $ $ $ $ $ $ $ $ = app.data.driver

但给出:

  AttributeError:'Flask'对象没有属性'data'

一旦我的应用程序创建了连接,应该如何在我的蓝图中找到它?



以下是完整的跟踪

  Traceback(最近一次调用最后一次):
在< module>文件中的login.py,第12行。
从子导入子
文件/home/xxx/xxx/child/child.py,第13行,在< module>
mongo = PyMongo(app)#blueprint
文件/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py,第97行,在__init__
self.init_app(app,config_prefix)
文件/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py,第121行,在init_app
raise Exception ('duplicate config_prefix'%s''%config_prefix)
异常:重复config_prefixMONGO
(xxx)xxx @ linux:〜/ xxx $ python login.py
Traceback最后调用):
在< module>文件中,输入login.py,第12行。
课程导入课程
在< module>文件中的/home/xxx/xxx/child/child.py,第13行。
mongo = PyMongo(app)#blueprint
文件/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py,第97行,在__init__
self.init_app(app,config_prefix)
文件/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py,第121行,在init_app
raise Exception ('duplicate config_prefix'%s''%config_prefix)
例外:重复config_prefixMONGO



<所以问题是如何将连接字符串构造到每个蓝图中的数据库。这里是文件结构:

  login.py 
config.py
/child/child.py
/child2/child2.py

这里是配置。 py

  MONGO_DBNAME ='xxx'

MONGO_URL = os.environ .get('MONGO_URL')
如果不是MONGO_URL:
MONGO_URL =mongodb:// xxx:xxxx@xxxx.mongolab.com:55822 / heroku_xxx;

MONGO_URI = MONGO_URL

我在下面的答案中试过了这个建议,但是这不起作用。

解决方案

在蓝图中执行导入的方法的一个问题是建议Emanuel Ey发现,它导致了一个循环的进口。大量的游戏后,事实证明唯一的办法(我能找到)是创建一个单独的文件,称为 database.py 连接到数据库,然后我可以导入连接到蓝图如下:

  child.py 

从数据库导入mongo
courses = Blueprint('courses',__name__)

和我的 database.py

  from flask.ext.pymongo import PyMongo 
mongo = PyMongo()

和应用程序login.py,但必须初始化数据库

 从数据库导入mongo 
app = Flask(__ name__)
app.config.from_object('config')
mongo.init_app应用程序)#初始化在这里!

从子导入子
从子导入2 child2

app.register_blueprint(child.child)
app.register_blueprint(child2.child2)


What is the correct way of picking up my mongo object in my Blueprints?

Here is how I have my parent login.py:

app.config.from_object('config')
from flask.ext.pymongo import PyMongo
from child import child
from child2 import child2


app = Flask(__name__)
app.register_blueprint(child2.child2)
app.register_blueprint(child.child)

in my child.py

from app import app
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child = Blueprint('child', __name__)

child2.py is the same structure as child:

from app import app
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child2 = Blueprint('child2', __name__)

Here is the error message:

raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"

I've tried the following in the blueprint

mongo = app.data.driver

but that gives:

AttributeError: 'Flask' object has no attribute 'data'

Once my app has created the connection, how should I pick it up in my blueprints?

Here is the full trace

Traceback (most recent call last):
  File "login.py", line 12, in <module>
    from child import child
  File "/home/xxx/xxx/child/child.py", line 13, in <module>
    mongo = PyMongo(app) #blueprint
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 121, in init_app
    raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"
(xxx)xxx@linux:~/xxx$ python login.py 
Traceback (most recent call last):
  File "login.py", line 12, in <module>
    from courses import courses
  File "/home/xxx/xxx/child/child.py", line 13, in <module>
    mongo = PyMongo(app) #blueprint
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 121, in init_app
    raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"

So the question is how can one structure the connection strings to the db in each of the blueprints. Here is the file structure:

login.py
config.py
/child/child.py
/child2/child2.py

here is the config.py

MONGO_DBNAME = 'xxx'

MONGO_URL = os.environ.get('MONGO_URL')
if not MONGO_URL:
    MONGO_URL = "mongodb://xxx:xxxx@xxxx.mongolab.com:55822/heroku_xxx";

MONGO_URI = MONGO_URL

I've tried the suggestion below in answers, but this did not work. See my comments below that prospective answer.

解决方案

One of the issues with the approach of performing an import in the blueprint as was suggest by Emanuel Ey, turns out that it causes a circular import. After much playing, it turns out that the only way (I could find) was to create a separate file called database.py that connects to the database and then I can import this connection to by blueprint as follows:

child.py

from database import mongo
courses = Blueprint('courses', __name__)

and my database.py

from flask.ext.pymongo import PyMongo
mongo = PyMongo() 

and the app, login.py but has to initialize the database

from database import mongo
app = Flask(__name__)
app.config.from_object('config')
mongo.init_app(app) # initialize here!

from child import child 
from child import2 child2

app.register_blueprint(child.child)
app.register_blueprint(child2.child2)

这篇关于蓝图,PyMongo在烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆