Using class based views.

29 views
Skip to first unread message

Ajay M

unread,
Feb 24, 2015, 12:05:51 AM2/24/15
to django...@googlegroups.com
Hi all,
I'm on a Django project, and I have written the following functions in my views.py.

def my_view_function_1(request):
    #Some code
    return HttpResponse(------)


def my_view_function_2(request):
    #Some code
    return HttpResponse(------)


def my_view_function_3(request):
    #Some code
    return HttpResponse(------)



Some of them are POST requests and some of them are GET.
I need to switch to class based views. Is there any ways to include multiple GET requests and POST requests in class based views?

James Schneider

unread,
Feb 24, 2015, 1:52:10 AM2/24/15
to django...@googlegroups.com

Sure, you would just override the get() and post() and possibly dispatch() methods in your classes as needed.

I always recommend the classy CBV inspector, helps immensely with understanding the available methods and inheritance in the CBV's.

http://ccbv.co.uk/

Also check out the source code of Django to see how they implement each of the generic CBV's on GitHub in django.views.generic.

However, if you have working view functions, what is the"need" to switch to CBV's?

FBV's may be more appropriate if you have a ton of logic to handle various types of requests via a single view, which sounds like it may be the case here. CBV'S don't pay out well in that regard from a simplicity standpoint, although they are more than capable.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9ea40ca1-387e-4234-be01-504886d7de07%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Javier Guerra Giraldez

unread,
Feb 24, 2015, 8:20:01 AM2/24/15
to django...@googlegroups.com
On Tue, Feb 24, 2015 at 1:51 AM, James Schneider
<jrschn...@gmail.com> wrote:
> However, if you have working view functions, what is the"need" to switch to
> CBV's?


this.

the switch to CBV, wasn't "modern Django views are classes instead of
functions; Get rid of your functions!, FBV are dead!". Instead it's
just that _generic_ views weren't useful enough, and to make them more
extensively configurable, they were transformed into a very capable,
albeit complex, set of classes.

all this is just for the included generic views, for your own views
just do whatever you find appropriate to structure your code and, in
the end, present a callable to the url() routing structures.

If you have too much repeated code, by all means factor it out; either
into supporting functions, or turn your views into objects, or
classes, anything you like. But even if you decide to do some CBV,
there's no need to use the included classes, as they're unlikely to be
the best answer outside of the 'generic view' envelope.

--
Javier
Reply all
Reply to author
Forward
0 new messages