librelist archives

« back to archive

Flask-PyMongo and Blueprint

Flask-PyMongo and Blueprint

From:
Nicholas
Date:
2013-08-21 @ 13:07
Hi, everyone..

I'm trying to start a project with Flask and Mongo but I don't seem to be
able to use Flask-PyMongo with Blueprints. Every street I take, it's a dead
end.

First, I tried what seemed logical to me. I imported PyMongo inside my
blueprint "app" and tried to use it. But it gave me an error that PyMongo
wasn't inside the blueprint extensions. Fine. Went to do some research..

Then I tried the method described here:
https://github.com/dcrosta/flask-pymongo/issues/11

I used application factory, assigned the db into the g object and so on...
But it says that PyMongo has no attribute 'config_prefix', even if I assign
a config_prefix manually.

I've tried more couple ways but I couldn't manage to make it work.

I`m sure this must be quite simple, can someone shed a light on me ?

Thanks in advance!

Re: [flask] Flask-PyMongo and Blueprint

From:
lynadge
Date:
2013-08-21 @ 13:18
Simply, you add after initialisation:

-----------------------------------------------
     pymongo = PyMongo(app)
     @app.before_request
     def before_request():
         g.mongo = pymongo
-----------------------------------------------

And then, in your blueprint you import the g object.

In the view you can use:

-----------------------------------------------
     g.mongo.db['<your document>']
-----------------------------------------------


Am 2013-08-21 14:07, schrieb Nicholas:
> Hi, everyone..
> 
> I'm trying to start a project with Flask and Mongo but I don't seem to
> be able to use Flask-PyMongo with Blueprints. Every street I take,
> it's a dead end.
> 
> First, I tried what seemed logical to me. I imported PyMongo inside my
> blueprint "app" and tried to use it. But it gave me an error that
> PyMongo wasn't inside the blueprint extensions. Fine. Went to do some
> research..
> 
> Then I tried the method described
> here: https://github.com/dcrosta/flask-pymongo/issues/11 [1]
> 
> I used application factory, assigned the db into the g object and so
> on... But it says that PyMongo has no attribute 'config_prefix', even
> if I assign a config_prefix manually.
> 
> I've tried more couple ways but I couldn't manage to make it work.
> 
> I`m sure this must be quite simple, can someone shed a light on me ?
> 
> Thanks in advance!
> 
> Links:
> ------
> [1] https://github.com/dcrosta/flask-pymongo/issues/11

Re: [flask] Flask-PyMongo and Blueprint

From:
Nicholas
Date:
2013-08-21 @ 13:24
Thanks so much!!!

I was doing this but was importing the `db` from another attempt, not from
the 'g' object.

Thanks again!


On Wed, Aug 21, 2013 at 2:18 PM, lynadge <lynadge@barfooz.de> wrote:

> Simply, you add after initialisation:
>
> -----------------------------------------------
>      pymongo = PyMongo(app)
>      @app.before_request
>      def before_request():
>          g.mongo = pymongo
> -----------------------------------------------
>
> And then, in your blueprint you import the g object.
>
> In the view you can use:
>
> -----------------------------------------------
>      g.mongo.db['<your document>']
> -----------------------------------------------
>
>
> Am 2013-08-21 14:07, schrieb Nicholas:
> > Hi, everyone..
> >
> > I'm trying to start a project with Flask and Mongo but I don't seem to
> > be able to use Flask-PyMongo with Blueprints. Every street I take,
> > it's a dead end.
> >
> > First, I tried what seemed logical to me. I imported PyMongo inside my
> > blueprint "app" and tried to use it. But it gave me an error that
> > PyMongo wasn't inside the blueprint extensions. Fine. Went to do some
> > research..
> >
> > Then I tried the method described
> > here: https://github.com/dcrosta/flask-pymongo/issues/11 [1]
> >
> > I used application factory, assigned the db into the g object and so
> > on... But it says that PyMongo has no attribute 'config_prefix', even
> > if I assign a config_prefix manually.
> >
> > I've tried more couple ways but I couldn't manage to make it work.
> >
> > I`m sure this must be quite simple, can someone shed a light on me ?
> >
> > Thanks in advance!
> >
> > Links:
> > ------
> > [1] https://github.com/dcrosta/flask-pymongo/issues/11
>

Re: [flask] Flask-PyMongo and Blueprint

From:
lynadge
Date:
2013-08-21 @ 13:30
No problem. I know, i had many small similar problems. :D

Am 2013-08-21 14:24, schrieb Nicholas:
> Thanks so much!!!
> 
> I was doing this but was importing the `db` from another attempt, not
> from the 'g' object.
> 
> Thanks again!
> 
> On Wed, Aug 21, 2013 at 2:18 PM, lynadge <lynadge@barfooz.de> wrote:
> 
>> Simply, you add after initialisation:
>> 
>> -----------------------------------------------
>>      pymongo = PyMongo(app)
>>      @app.before_request
>>      def before_request():
>>          g.mongo = pymongo
>> -----------------------------------------------
>> 
>> And then, in your blueprint you import the g object.
>> 
>> In the view you can use:
>> 
>> -----------------------------------------------
>>      g.mongo.db['<your document>']
>> -----------------------------------------------
>> 
>> Am 2013-08-21 14:07, schrieb Nicholas:
>> 
>>> Hi, everyone..
>>> 
>>> I'm trying to start a project with Flask and Mongo but I don't
>> seem to
>>> be able to use Flask-PyMongo with Blueprints. Every street I
>> take,
>>> it's a dead end.
>>> 
>>> First, I tried what seemed logical to me. I imported PyMongo
>> inside my
>>> blueprint "app" and tried to use it. But it gave me an error that
>>> PyMongo wasn't inside the blueprint extensions. Fine. Went to do
>> some
>>> research..
>>> 
>>> Then I tried the method described
>>> here: https://github.com/dcrosta/flask-pymongo/issues/11 [1] [1]
>> 
>>> 
>>> I used application factory, assigned the db into the g object and
>> so
>>> on... But it says that PyMongo has no attribute 'config_prefix',
>> even
>>> if I assign a config_prefix manually.
>>> 
>>> I've tried more couple ways but I couldn't manage to make it
>> work.
>>> 
>>> I`m sure this must be quite simple, can someone shed a light on
>> me ?
>>> 
>>> Thanks in advance!
>>> 
>>> Links:
>>> ------
>>> [1] https://github.com/dcrosta/flask-pymongo/issues/11 [1]
> 
> 
> 
> Links:
> ------
> [1] https://github.com/dcrosta/flask-pymongo/issues/11