librelist archives

« back to archive

class based view in Flask

class based view in Flask

From:
weasky
Date:
2011-10-01 @ 02:07
Hi guys,
I am wondering if I can do class-based view in Flask? For example,
like UserView in following, we will have
'/user','/user/login','/user/register','/user/add' exposed as urls.

Thanks, I got the snippet from uliweb.



@app.route('/user')
class UserView(object):
    def __begin__(self):
        if not request.user:
            return redirect('/login?next=%s' % request.path)

    @app.route('/login')
    def login(self):
        #login process
        #URL = /login

    def register(self, name):
        #register process
        #URL = /user/register/<name>

    @app.route('add')
    def add_user(self):
        #add process
        #URL = /user/add

    @app.route('')
    def list(self):
        #URL = /user here '' will just user UserView class URL prefix '/user'

    def _common(self):
        #this function will not be exposed

Re: [flask] class based view in Flask

From:
Armin Ronacher
Date:
2011-10-01 @ 10:50
Hi,

On 2011-10-01 4:07 AM, weasky wrote:
> Hi guys,
> I am wondering if I can do class-based view in Flask? For example,
 > like UserView in following, we will have'/user','/user/login',
 > '/user/register','/user/add'  exposed as urls.
http://flask.pocoo.org/docs/views/#method-views-for-apis


Regards,
Armin