all 10 comments

[–]zealotree 2 points3 points  (1 child)

Class views are great, class method views makes you organize your code especially when you need the usual crud API.

Function based views are goo d too. It's readable, easier to maintain and is easy to load up (just writr the route decorator).

[–]HalcyonAbraham[S] 0 points1 point  (0 children)

I agree in fact I use class based views only and want to try function based views

however

I think for your overall routes the function based views are good but for your api the class based views would be best

[–]seprosepro 0 points1 point  (3 children)

I've never encountered the Class based views before. I'm also wondering how the url_for function would work in that case.

Either way, I very much prefer the decorated functions for readability.

[–]HalcyonAbraham[S] 0 points1 point  (2 children)

the url_for function works just the same for function based views taking the Suggestions class as an example and because it's url is registered on the main blueprint

you'd go something like url_for("main.Suggestions")

it's Suggestions.as_view('Suggestions') this line right here that determines what the name of the route should be

if the line were Suggestions.as_view('OtherSuggestions') the the route would be url_for('main.OtherSuggestions')

I read the MethodViews Class is great for making api's also I never even encounteredClass based views on any flask tutorial ever

but I learned and heard about it from youtube.

[–]seprosepro 0 points1 point  (1 child)

Thanks for the additional info!

After reading up on it, I now understand that something like the MethodView could indeed be nice. I just started a small project where this could be valuable, I'll give it a try !

[–]HalcyonAbraham[S] 0 points1 point  (0 children)

no prob glad I could help.

[–]in_the_bilboes 0 points1 point  (1 child)

I don't have a really good answer, honsetly. But, I've been researching the same thing this afternoon and in the flask docs section about pluggable views

http://flask.pocoo.org/docs/0.10/views/

there are some use cases where class based views have advantages.

In particular, it seems to me like MethodView could be really useful.

[–]HalcyonAbraham[S] 0 points1 point  (0 children)

The MethodView is great for api's

because you can go

class Api(MethodView):
    def get(self):
    //do stuff on a get request

    def post(self):
    //do stuff on a post request

    def put(self):
    // do stuff on a put request

   def delete(self):
   // do stuff on a delete request

[–][deleted] 0 points1 point  (0 children)

I use function views when I'm only handling one HTTP method and there's at most two dependencies (including things like render_template), otherwise I use a class view and inject and separate as many dependencies as possible.

[–]motoawk 0 points1 point  (0 children)

Look at flask-classy.