librelist archives

« back to archive

Problem with Flask-SQLAlchemy and user login

Problem with Flask-SQLAlchemy and user login

From:
Tobias Siebenlist
Date:
2011-01-11 @ 08:39
Hi.

I started using Flask and like it very much.
Trying the Flask-SQLAlchemy extension I ran into the problem, that i
cannot sign in a user.
Doing the same thing on a terminal with the same commands/functions
just works fine.
Maybe someone has an idea why the login function fails and tells me
that the password is invalid?
I pasted the source at http://paste.pocoo.org/show/FIixuEpO8tH3h6wbopes/

Regards,
Tobi

[flask] problem with modules, routes and names of methods

From:
rocco storm
Date:
2011-01-12 @ 14:02
Hi there,

i have a little problem with modules and routes in an Flask-app

The structure of my application looks something like this:
------------------------------------------
/myapp
     __init__.py
     /apps
         __init__.py
         /app1
             __init__.py
             views.py
         /app2
             __init__.py
             views.py
------------------------------------------

in the __init__.py, I register the different  modules:
---------------------------------------------------
from myapp.apps.app1 import views
from myapp.apps.app2 import views

app = Flask(__name__)
app.register_module(app1, url_prefix="/app1")
app.register_module(app2, url_prefix="/app2")
---------------------------------------------------

i assume that i have 2 totally independent apps, witch are separated by 
the prefix.

The problem is, when i have methods with the same name in app1 and app2,
they always point to the module that is registered last.

for example, the views.py look like this:

app1/views.py
--------------------
@app1.route('/')
def index():
     return "app1 index"
---------------------

app2/views.py
---------------------
@app2.route('/')
def index():
     return "app2 index"
----------------------

then 'http://localhost/app1/' and 'http://localhost/app2/' both return 
'app2 index'.

How can i prevent this? I think putting the namespace a second  time in 
the method-names like 'def <APP-NAME>_index():' is a ugly solution.

Any ideas?

Best regards,
r.

Re: [flask] problem with modules, routes and names of methods

From:
eduardo
Date:
2011-01-12 @ 14:43
Em 12/1/2011 12:02, rocco storm escreveu:
> Hi there,
>
> i have a little problem with modules and routes in an Flask-app
>
> The structure of my application looks something like this:
> ------------------------------------------
> /myapp
>       __init__.py
>       /apps
>           __init__.py
>           /app1
>               __init__.py
>               views.py
>           /app2
>               __init__.py
>               views.py
> ------------------------------------------
>
> in the __init__.py, I register the different  modules:
> ---------------------------------------------------
> from myapp.apps.app1 import views
> from myapp.apps.app2 import views
>

Besides I don't now how yours 'views.py' were constructed but, lets see...

/If in your 'views.py' you have something like that

from flask import ....

@views.route('/')
def index():
     .../

try doing

/from flask import ....
/*from flask import Module*
/
*views = Module(__name__)*

@views.route('/')
def index():
     ...
/
then import
/
    from myapp.apps.app1.views import views as views1
    from myapp.apps.app2.views import views as views2


/
> app.register_module(app1, url_prefix="/app1")
> app.register_module(app2, url_prefix="/app2")
> ---------------------------------------------------
>

and then

app.register_module(views1, ...)
app.register_module(views2, ...)


I hope this can help.

regards,

   eduardo

Re: [flask] problem with modules, routes and names of methods

From:
rocco storm
Date:
2011-01-12 @ 15:53
Hi Eduardo,

as you see in my full example at 
https://bitbucket.org/rocco_storm/flask_bsp/src,
it is nearly the same as your advice. And it don't work.

Regards,
Robert


Am 12.01.2011 15:43, schrieb eduardo:
> Em 12/1/2011 12:02, rocco storm escreveu:
>> Hi there,
>>
>> i have a little problem with modules and routes in an Flask-app
>>
>> The structure of my application looks something like this:
>> ------------------------------------------
>> /myapp
>>       __init__.py
>>       /apps
>>           __init__.py
>>           /app1
>>               __init__.py
>>               views.py
>>           /app2
>>               __init__.py
>>               views.py
>> ------------------------------------------
>>
>> in the __init__.py, I register the different  modules:
>> ---------------------------------------------------
>> from myapp.apps.app1 import views
>> from myapp.apps.app2 import views
>>
>
> Besides I don't now how yours 'views.py' were constructed but, lets see...
>
> /If in your 'views.py' you have something like that
>
> from flask import ....
>
> @views.route('/')
> def index():
>     .../
>
> try doing
>
> /from flask import ....
> /*from flask import Module*
> /
> *views = Module(__name__)*
>
> @views.route('/')
> def index():
>     ...
> /
> then import
> /
>    from myapp.apps.app1.views import views as views1
>    from myapp.apps.app2.views import views as views2
>
>
> /
>> app.register_module(app1, url_prefix="/app1")
>> app.register_module(app2, url_prefix="/app2")
>> ---------------------------------------------------
>>
>
> and then
>
> app.register_module(views1, ...)
> app.register_module(views2, ...)
>
>
> I hope this can help.
>
> regards,
>
>   eduardo
>

Re: [flask] problem with modules, routes and names of methods

From:
eduardo
Date:
2011-01-12 @ 17:56
Hi Robert,

To me, it seems that for some reason Flask gets confused about equal 
names :(

I don't now exactly what happens but if you rename 'app1/views.py' and/or
'app2/views.py' to respectively 'app1.whatever1.py' and 'app1.whatever2.py'
it will work.

Well... almost.

Another thing that I realized is that Flask needs to now (I'm guessing 
here), is where
to when you ask for "http://127.0.0.1:5000".

Long story short: change flask_bsp/__init__.py from this:


from flask import Flask
from flask_bsp.apps.app1.*views *import app1
from flask_bsp.apps.app2.*views *import app2

app = Flask(__name__)

app.register_module(app1, url_prefix="/app1")
app.register_module(app2, url_prefix="/app2")


to this


from flask import Flask
from flask_bsp.apps.app1.*whatever1* import app1
from flask_bsp.apps.app2.*whatever2 *import app2

app = Flask(__name__)

*app.register_module(app1)*
app.register_module(app1, url_prefix="/app1")
app.register_module(app2, url_prefix="/app2")


*Another thing: don't forget remove all '*.pyc' files before you try it.*


regards,

  eduardo

> Hi Eduardo,
>
> as you see in my full example at 
> https://bitbucket.org/rocco_storm/flask_bsp/src,
> it is nearly the same as your advice. And it don't work.
>
> Regards,
> Robert
>
>
> Am 12.01.2011 15:43, schrieb eduardo:
>> Em 12/1/2011 12:02, rocco storm escreveu:
>>> Hi there,
>>>
>>> i have a little problem with modules and routes in an Flask-app
>>>
>>> The structure of my application looks something like this:
>>> ------------------------------------------
>>> /myapp
>>>       __init__.py
>>>       /apps
>>>           __init__.py
>>>           /app1
....

Re: [flask] problem with modules, routes and names of methods

From:
Mathias Nielsen
Date:
2011-01-13 @ 09:03
Hi Robert.
I use the same structure in my project.

Try doing this in your views.py

app1 = Module(__name__,'app1')

Somehow the normal approach names all modules 'views' since that is the
__name__ of the module when declared. Or something along those lines :)

2011/1/12 eduardo <eduardo.manso@gmail.com>

>  Hi Robert,
>
> To me, it seems that for some reason Flask gets confused about equal names
> :(
>
> I don't now exactly what happens but if you rename 'app1/views.py' and/or
> 'app2/views.py' to respectively 'app1.whatever1.py' and 'app1.whatever2.py'
>
> it will work.
>
> Well... almost.
>
> Another thing that I realized is that Flask needs to now (I'm guessing
> here), is where
> to when you ask for "http://127.0.0.1:5000" <http://127.0.0.1:5000>.
>
> Long story short: change flask_bsp/__init__.py from this:
>
>
> from flask import Flask
> from flask_bsp.apps.app1.*views *import app1
> from flask_bsp.apps.app2.*views *import app2
>
> app = Flask(__name__)
>
>
> app.register_module(app1, url_prefix="/app1")
> app.register_module(app2, url_prefix="/app2")
>
>
> to this
>
>
> from flask import Flask
> from flask_bsp.apps.app1.*whatever1* import app1
> from flask_bsp.apps.app2.*whatever2 *import app2
>
> app = Flask(__name__)
>
> *app.register_module(app1)*
>
> app.register_module(app1, url_prefix="/app1")
> app.register_module(app2, url_prefix="/app2")
>
>
> *Another thing: don't forget remove all '*.pyc' files before you try it.*
>
>
> regards,
>
>  eduardo
>
>
>  Hi Eduardo,
>
> as you see in my full example at
> https://bitbucket.org/rocco_storm/flask_bsp/src,
> it is nearly the same as your advice. And it don't work.
>
> Regards,
> Robert
>
>
> Am 12.01.2011 15:43, schrieb eduardo:
>
> Em 12/1/2011 12:02, rocco storm escreveu:
>
> Hi there,
>
> i have a little problem with modules and routes in an Flask-app
>
> The structure of my application looks something like this:
> ------------------------------------------
> /myapp
>      __init__.py
>      /apps
>          __init__.py
>          /app1
>
>   ....
>



-- 
Best regards

Mathias Nielsen
cohida.com (website currently under reconstruction)
(0045)61264334

Re: [flask] problem with modules, routes and names of methods

From:
rocco storm
Date:
2011-01-13 @ 14:42
Hell yeah, this way it works.

Thank you!

Am 13.01.2011 10:03, schrieb Mathias Nielsen:
> Hi Robert.
> I use the same structure in my project.
>
> Try doing this in your views.py
>
> app1 = Module(__name__,'app1')
>
> Somehow the normal approach names all modules 'views' since that is 
> the __name__ of the module when declared. Or something along those 
> lines :)
>
> 2011/1/12 eduardo <eduardo.manso@gmail.com 
> <mailto:eduardo.manso@gmail.com>>
>
>     Hi Robert,
>
>     To me, it seems that for some reason Flask gets confused about
>     equal names :(
>
>     I don't now exactly what happens but if you rename 'app1/views.py'
>     and/or
>     'app2/views.py' to respectively 'app1.whatever1.py
>     <http://app1.whatever1.py>' and 'app1.whatever2.py
>     <http://app1.whatever2.py>'
>     it will work.
>
>     Well... almost.
>
>     Another thing that I realized is that Flask needs to now
>     (I'm guessing here), is where
>     to when you ask for "http://127.0.0.1:5000" <http://127.0.0.1:5000>.
>
>     Long story short: change flask_bsp/__init__.py from this:
>
>
>     from flask import Flask
>     from flask_bsp.apps.app1.*views *import app1
>     from flask_bsp.apps.app2.*views *import app2
>
>     app = Flask(__name__)
>
>
>     app.register_module(app1, url_prefix="/app1")
>     app.register_module(app2, url_prefix="/app2")
>
>
>     to this
>
>
>     from flask import Flask
>     from flask_bsp.apps.app1.*whatever1* import app1
>     from flask_bsp.apps.app2.*whatever2 *import app2
>
>     app = Flask(__name__)
>
>     *app.register_module(app1)*
>
>     app.register_module(app1, url_prefix="/app1")
>     app.register_module(app2, url_prefix="/app2")
>
>
>     *Another thing: don't forget remove all '*.pyc' files before you
>     try it.*
>
>
>     regards,
>
>      eduardo
>
>
>>     Hi Eduardo,
>>
>>     as you see in my full example at
>>     https://bitbucket.org/rocco_storm/flask_bsp/src,
>>     it is nearly the same as your advice. And it don't work.
>>
>>     Regards,
>>     Robert
>>
>>
>>     Am 12.01.2011 15:43, schrieb eduardo:
>>>     Em 12/1/2011 12:02, rocco storm escreveu:
>>>>     Hi there,
>>>>
>>>>     i have a little problem with modules and routes in an Flask-app
>>>>
>>>>     The structure of my application looks something like this:
>>>>     ------------------------------------------
>>>>     /myapp
>>>>           __init__.py
>>>>           /apps
>>>>               __init__.py
>>>>               /app1
>     ....
>
>
>
>
> -- 
> Best regards
>
> Mathias Nielsen
> cohida.com <http://cohida.com> (website currently under reconstruction)
> (0045)61264334

Re: [flask] problem with modules, routes and names of methods

From:
Anton Khodakivskiy
Date:
2011-01-12 @ 14:14
How do you instantiate app1 and app2? Can you compare them? Maybe they 
actually point to the same object...

On Jan 12, 2011, at 4:02 PM, rocco storm wrote:

> Hi there,
> 
> i have a little problem with modules and routes in an Flask-app
> 
> The structure of my application looks something like this:
> ------------------------------------------
> /myapp
>     __init__.py
>     /apps
>         __init__.py
>         /app1
>             __init__.py
>             views.py
>         /app2
>             __init__.py
>             views.py
> ------------------------------------------
> 
> in the __init__.py, I register the different  modules:
> ---------------------------------------------------
> from myapp.apps.app1 import views
> from myapp.apps.app2 import views
> 
> app = Flask(__name__)
> app.register_module(app1, url_prefix="/app1")
> app.register_module(app2, url_prefix="/app2")
> ---------------------------------------------------
> 
> i assume that i have 2 totally independent apps, witch are separated by 
> the prefix.
> 
> The problem is, when i have methods with the same name in app1 and app2,
> they always point to the module that is registered last.
> 
> for example, the views.py look like this:
> 
> app1/views.py
> --------------------
> @app1.route('/')
> def index():
>     return "app1 index"
> ---------------------
> 
> app2/views.py
> ---------------------
> @app2.route('/')
> def index():
>     return "app2 index"
> ----------------------
> 
> then 'http://localhost/app1/' and 'http://localhost/app2/' both return 
> 'app2 index'.
> 
> How can i prevent this? I think putting the namespace a second  time in 
> the method-names like 'def <APP-NAME>_index():' is a ugly solution.
> 
> Any ideas?
> 
> Best regards,
> r.

Re: [flask] problem with modules, routes and names of methods

From:
rocco storm
Date:
2011-01-12 @ 14:42
I put a full example to bitbucket:

https://bitbucket.org/rocco_storm/flask_bsp/src

In this example, if i point to 'http://localhost/app1', 'app2 index' is 
returned.



Am 12.01.2011 15:14, schrieb Anton Khodakivskiy:
> How do you instantiate app1 and app2? Can you compare them? Maybe they 
actually point to the same object...
>
> On Jan 12, 2011, at 4:02 PM, rocco storm wrote:
>
>> Hi there,
>>
>> i have a little problem with modules and routes in an Flask-app
>>
>> The structure of my application looks something like this:
>> ------------------------------------------
>> /myapp
>>      __init__.py
>>      /apps
>>          __init__.py
>>          /app1
>>              __init__.py
>>              views.py
>>          /app2
>>              __init__.py
>>              views.py
>> ------------------------------------------
>>
>> in the __init__.py, I register the different  modules:
>> ---------------------------------------------------
>> from myapp.apps.app1 import views
>> from myapp.apps.app2 import views
>>
>> app = Flask(__name__)
>> app.register_module(app1, url_prefix="/app1")
>> app.register_module(app2, url_prefix="/app2")
>> ---------------------------------------------------
>>
>> i assume that i have 2 totally independent apps, witch are separated by
>> the prefix.
>>
>> The problem is, when i have methods with the same name in app1 and app2,
>> they always point to the module that is registered last.
>>
>> for example, the views.py look like this:
>>
>> app1/views.py
>> --------------------
>> @app1.route('/')
>> def index():
>>      return "app1 index"
>> ---------------------
>>
>> app2/views.py
>> ---------------------
>> @app2.route('/')
>> def index():
>>      return "app2 index"
>> ----------------------
>>
>> then 'http://localhost/app1/' and 'http://localhost/app2/' both return
>> 'app2 index'.
>>
>> How can i prevent this? I think putting the namespace a second  time in
>> the method-names like 'def<APP-NAME>_index():' is a ugly solution.
>>
>> Any ideas?
>>
>> Best regards,
>> r.
>