librelist archives

« back to archive

Class based views in blueprints

Class based views in blueprints

From:
adam.pointer@gmx.com
Date:
2012-02-01 @ 20:47
I am quite comfortable using blueprints in developing my app and I am 
pleased with the overall structure of the codebase. Only thing is I would 
like to use a more OOP approach in my views which at the moment are a 
bunch of functions in my blueprint modules. Can class based views and 
blueprints work together? I can't figure out how to register the view and 
blueprint. It seems you can only use one or the other.

Re: [flask] Class based views in blueprints

From:
Smartboy
Date:
2012-02-02 @ 08:47
You can, but you can't use the method decorator. You have to use
blueprint.add_url_rule() as described here for app:
http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule in your init
method.

Smartboy

On Wed, Feb 1, 2012 at 12:47 PM, adam.pointer@gmx.com
<adam.pointer@gmx.com>wrote:

> I am quite comfortable using blueprints in developing my app and I am
> pleased with the overall structure of the codebase. Only thing is I would
> like to use a more OOP approach in my views which at the moment are a bunch
> of functions in my blueprint modules. Can class based views and blueprints
> work together? I can't figure out how to register the view and blueprint.
> It seems you can only use one or the other.
>
>

Re: [flask] Class based views in blueprints

From:
Adam Pointer
Date:
2012-02-02 @ 09:09
So in my blueprint's init function I would do something like this 
(plagiarised from documentation):

simple_page = Blueprint('simple_page', __name__)

class UserAPI(MethodView):

     def get(self):
         users = User.query.all()

     def post(self):
         user = User.from_form_data(request.form)

simple_page .add_url_rule('/', view_func=UserAPI.as_view('users'))

Then in the main application entry-point register the blueprint with the 
flask object as normal?

On 02/02/12 08:47, Smartboy wrote:
> You can, but you can't use the method decorator. You have to use 
> blueprint.add_url_rule() as described here for app: 
> http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule in your init 
> method.
>
> Smartboy
>
> On Wed, Feb 1, 2012 at 12:47 PM, adam.pointer@gmx.com 
> <mailto:adam.pointer@gmx.com> <adam.pointer@gmx.com 
> <mailto:adam.pointer@gmx.com>> wrote:
>
>     I am quite comfortable using blueprints in developing my app and I
>     am pleased with the overall structure of the codebase. Only thing
>     is I would like to use a more OOP approach in my views which at
>     the moment are a bunch of functions in my blueprint modules. Can
>     class based views and blueprints work together? I can't figure out
>     how to register the view and blueprint. It seems you can only use
>     one or the other.
>
>