CBV – View

View is the ancestor of ALL Django CBV. From the great site Classy Class Based Views, they are described as

Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking.

This is no joke. The View class has almost nothing to it, but it’s a solid foundation for everything else that will be done.

Its implementation has just one attribute http_method_names which is a list that allows you to specify what http verbs are allowed.

Other than that, there’s really not much to it. You just write a simple method, something like this:

def get(self, _):
    return HttpResponse('My Content')

All that gets returned to the page is a simple HTML. You can specify the content_type if you just want to return JSON or plain text but defining the content_type like this:

def get(self, _):
    return HttpResponse('My Content', content_type='text plain')

You can also make the text that is displayed be based on a variable defined in the class.

First, you need to define the variable

content = 'This is a {View} template and is not used for much of anything but ' \
              'allowing extensions of it for other Views'

And then you can do something like this:

def get(self, _):
    return HttpResponse(self.content, content_type='text/plain')

Also, as mentioned above you can specify the allowable methods via the attribute http_method_names.

The following HTTP methods are allowed:

  • get
  • post
  • put
  • patch
  • delete
  • head
  • options
  • trace

By default all are allowed.

If we put all of the pieces together we can see that a really simple View CBV would look something like this:

class myView(View):
    content = 'This is a {View} template and is not used for much of anything but ' \
              'allowing extensions of it for other Views'
    http_method_names = ['get']

    def get(self, _):
        return HttpResponse(self.content, content_type='text/plain')

This View will return content to the page rendered as plain text. This CBV is also limited to only allowing get requests.

Here’s what it looks like in the browser:

Conclusion

View doesn’t do much, but it’s the case for everything else, so understanding it is going to be important.

Class Based Views

As I’ve written about previously I’m working on a Django app. It’s in a pretty good spot (you should totally check it out over at StadiaTracker.com) and I thought now would be a good time to learn a bit more about some of the ways that I’m rendering the pages.

I’m using Class Based Views (CBV) and I realized that I really didn’t grok how they worked. I wanted to change that.

I’ll be working on a series where I deep dive into the CBV and work them from several angles and try to get them to do all of the things that they are capable of.

The first place I’d suggest anyone start to get a good idea of CBV, and the idea of Mixins would be SpaPas’ GitHub Page where he does a really good job of covering many pieces of the CBV. It’s a great resource!

This is just the intro to this series and my hope is that I’ll publish one of these pieces each week for the next several months as I work my way through all of the various CBV that are available.