Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: class views, generic views, etc

Tags:

python

django

I'm coming back to Django after a brief encounter with version 1.2, and now in version 1.3 the favored approach to views seems to be using classes.

Keeping in mind code style, maintainability and modularity: when should I use classes, and when functions? Should I always extend from generic class views (there seems to be no harm in using TemplateView), or should I use my own view callable objects?

Thanks in advance.

like image 964
slezica Avatar asked Jun 09 '11 00:06

slezica


People also ask

What are the types of views in Django?

It will also explore the two major types of views: class-based and function-based views.

What are Django generic views?

Django's generic views were developed to ease that pain. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code.

Which view is better in Django?

1. Function-Based Views. Function-based views are good for beginners. It is very easy to understand in comparison to class-based views.

Are class-based views better Django?

This is why Django developers decided to add support for class-based views (CBVs). CBVs utilize OOP principles, which allow us to use inheritance, reuse code, and generally write better and cleaner code. We need to keep in mind that CBVs are not designed to replace FBVs.


2 Answers

I find that class-based views help keep my code readable and streamlined.

Take, for example, the sample (function-based) view from the form documentation:

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })

In the class-based equivalent, the boilerplate code and the nasty nested ifs disappear:

class Contact(FormView):
    template_name = 'contact.html'
    form_class = ContactForm
    success_url = '/thanks/'

    def form_valid(self, form):
        # Process the data in form.cleaned_data
        return super(Contact, self).form_valid(form)

Also, while you can always mix and match function-based and class-based views, I find that mixing styles in the same Django app tends to look quite messy. So, once I have a few views that really benefit from going class-based, it is an easy step to switch all the lot.

like image 173
tawmas Avatar answered Oct 26 '22 22:10

tawmas


There are in my opinion two cases for necessity of class-based(-generic)-views:

  • You really need generic functionality in your views and a little bit extra.
  • You write a resusable Django app and want to make it possible that others can extend your views.

For anything else use what you feel most comfortable with. As you said you basically good to go with extending from TemplateView and overwriting respective methods, though you could also use a function-based approach (and have to deal with render template calls by yourself). That's up to you in the end.

EDIT: One other advantage of class based views is that it lets you separate your code according to the request.method in a more cleaner manner and even returns 405 Method Not Allowed response codes when the wrong method is used. So you don't have to deal with lines like if request.method=='POST' or if request.method=='GET' at all and just extend a post or get method.

like image 41
Torsten Engelbrecht Avatar answered Oct 26 '22 23:10

Torsten Engelbrecht