The View Class

The View class is the basis for all of the other Django class-based view classes. For the most part, it's a remarkably simple class with only a few methods. You can take a look at the class if you're curious.

Also, all CBV's use their .as_view() method in URLs, so get used to typing that. You can turn that into a variable (my_view = MyView.as_view()) and then link that in your URL but that is often seen as redundant and pointless.

0:00

Jin goes class base
views come into flavors,

0:03

a generic class base views and well,
the few classes they all extend.

0:07

Okay so, a single class
might not count as a flavor.

0:11

While the view class might
not be the simplest to use,

0:14

its moment the least amount of
special bits to understand.

0:16

So I think it's a good place to start.

0:18

I've created an app for
tracking kids soccer teams.

0:20

I'll be working on this
out throughout the course.

0:22

So, let me help over the work spaces,
show you the app, and

0:25

start in on the view class.

0:27

So first,
let me walk you through this project.

0:29

It's a pretty simple project.

0:31

It tracks teams and players for
like a kids soccer league.

0:35

You could use it for adults soccer leagues
too but my idea was it was for kids.

0:40

So I've got everything set up here in the
admin, so let me hop over here to slash

0:46

admin, and I log in.

0:51

The username and password by the way
should all ready be stored in

0:55

the database, and
they are listed here in this read me file.

1:01

So you can log in using those
credentials if you want.

1:05

So here in the admin,
we can add teams and we can add players,

1:10

all those kinds of stuff so
like I'm gonna add a new team,

1:12

I could add say the Raptors,
and I'll be their coach,

1:17

sure, and
we're gonna be at the Downtown Gym.

1:22

And you can see here,
I can add players, pretty easily too,

1:25

if I wanted to add Andrew's a player and

1:29

he's 10 and he's gonna play Wide midfield.

1:36

And Lacey is a player and she's 9 and
she's gonna play Sweeper.

1:43

So to say that, and now get a second team.

1:47

There are a couple of front
facing pages as well.

1:50

Actually, let's just take this off.

1:54

So this is the home page is
just rendering a template.

1:57

If I go to slash teams,
you can see a couple of teams here.

2:02

And if I go look at the Raptors,

2:03

this is the team I just created,
there's all their information.

2:07

So yeah, site works,
you can ignore all of this stuff.

2:11

I just wanted to give you the idea of
a site in the middle of building a site.

2:18

Not everything works,
most things don't, but

2:20

you can feel free to add whatever
features you wanna add here.

2:25

Now though, let me show you some of
what's actually here in the file.

2:28

So if I go here in the Djangoal,
the stub, and

2:34

I go look in the views.py,
you see that there is a view here.

2:37

It's just rendering a template.

2:38

Like literally, it does nothing else,
not even a context dictionary, so

2:42

I need just rendering that one template.

2:45

This would be a good view to use
to show you the template view, but

2:49

I think I can wait to do
that till the next video.

2:52

For now though,

2:53

I wanna show you how to make just a simple
view that just sends out the word hello.

2:58

Like you do for every new thing you learn,
you're supposed to say hello world.

3:02

So I'm gonna do that
here in this views class.

3:04

So the first thing I need to do is,
I need to import the view class.

3:09

So, from django.views.generic import View,
all right.

3:17

And then, I need to make a new
class that extends this view class.

3:22

So, class HelloWorldView,
and it's a (View):.

3:27

Now this is as name is a 100% optional,
you can name as whatever you want.

3:31

I just like using things,
you'll see later on if I do a create view,

3:36

I was put in like create or create view in
the name, just so that I know what it is.

3:41

Now each view with lowercase v,
that's based on a view with a capital V,

3:47

automatically accepts the HTTP git method.

3:50

To handle that kind of request though, you
need to create the get method on the class

3:55

and I need to return an HTTP response.

3:57

So I should probably import that too.

4:01

So let's do that, from django.http
import HttpResponse, all right.

4:07

And then,
back down here in our HelloWorldView.

4:10

I’m gonna put in dev get(self, request):.

4:14

Takes two arguments,
self because it's an instance and

4:17

request because it gets a request,
just like function based view div.

4:21

And I'm gonna return the HttpResponse, and

4:25

I'm just gonna return
the string ("Hello World").

4:32

And let's put an exclamation mark in there
cuz you should have exclamation marks.

4:36

That's it, that's the entire view.

4:38

But having a view doesn't do me any
good without having the URL, right?

4:43

So URL's for class based views are just
a little bit different than they are for

4:48

function based views.

4:49

Since this view is in my global,
for lack of a better word views,

4:54

I'm gonna add the URL to
the global URL's file.

4:58

So this one out here that Django
automatically creates for me.

5:01

I've all ready imported views because
I wanted that home view, right?

5:07

Okay so, let's go ahead and let me go
ahead and add this right here, and

5:12

I'm gonna do (r'^hello/$', and
then just like with function based views,

5:19

I'm going to add the name of the view,
so HelloWorldView.

5:24

But I can't just point to a class,
I need to point to a method or

5:29

a function because that's how URL's work,
they have to point to a callable,

5:33

and classes by themselves are callable.

5:35

So I'll point to this as viewClass method,

5:41

sorry, it doesn't transform
the class into a method but

5:47

it handles instantiating an instance of
the class and doing all of the responsive

5:51

work that needs to be done to
turn the class into a function.

5:56

Okay, and
then I'll give it the name = 'hello'.

6:00

All right, so save that file.

6:01

Save everything.

6:03

So, I want to talk a little bit more
about what this does really quick.

6:07

So we call this method this as view,
and it's a class method so

6:12

that means it doesn't need
an instance of the class to run.

6:15

It creates an instance of the class, sets
up some different things like the request

6:19

object and all that stuff, and
then it runs the classes dispatch method.

6:23

And the dispatch method is
the method that's responsible for

6:26

running the correct method
based on the HTTP request.

6:30

So in this case,

6:30

when I make a get request, the dispatch
method will run the get method.

6:35

Now, knowing all the bits and pieces of
the stance isn't really important though,

6:39

unless or maybe until you're gonna start
messing around with dispatch on a view.

6:45

That is required sometimes though so
just kind of keep in mind that dispatch is

6:49

responsible for
handling HTTP methods appropriately, okay.

6:54

So view created, URL created, and
pointed to the views as view method.

6:59

Time to test it up.

7:01

So pop over here and instead of teams two,
I'm gonna go to hello.

7:06

There's my hello world and it works.

7:07

That's awesome.

7:09

The view class is a great resource
we needed to control everything and

7:12

starts at the bottom of class based views.

7:15

If you like me though, you won't
find yourself using it very often.

7:18

I've always found more use in the generic
views that are aimed to solving

7:20

specific problems.

7:22

So let me leave the low level behind,
move a bit further up the path, and