#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Refactor function-based into class-based views
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Class-based views. As we saw in the last video writing all these function-based views some repetition sneaks in like, getting the quote object
0:12
with get_object_or_404 and we did that at least four times. Same with the form. Form is valid save that happened both in new as in edit.
0:27
Let me introduce you to class-based views. The main takeaway from class-based views is that it allows you to structure your views
0:36
and reuse code by harnessing inheritance and mixins. And in this video I will re-write the views you've built so far
0:45
and you will see that that reduces the code dramatically and that is because we will inherit from Django's class-based views.
0:52
So let's open a class-based py file and we will later move that back into views, but just do see that side-by-side. First, I need to do some imports.
1:17
And here you see the generic class-based views we're going to inherit from. So we have list, detail, create, publish and delete
1:23
which closely matches the five functions we wrote here. You need our model. And now we can write out the class-based views.
1:42
And here I'm inheriting from ListView. And to get a list of all the quotes you only have to give the model. That was easy, right?
1:56
Well, it gets even better. I'm starting to like these class-based views. I mean, I don't have to write all this logic
2:16
and we'll see the magic in a bit. So actually by naming these templates consistently Django just knows what to do. So I have to highlight
2:27
that you have to use those conventions. A quote_list, quote_detail. Here we have a CreateView. And here I have to specify the fields.
2:57
It's just very similar to the forms up high we created earlier. And I have to give it a success_url. And I'm going to use the reverse_lazy helper.
3:19
That's the QuoteUpdate. It inherents from UpdateView. That's actually the same.
3:36
I'm less worried about this duplication which is only three lines instead of this whole code block. And we have QuoteDelete.
4:14
And that's it. Those are my class-based views. In the repo you will see the underscore cb, or class-based and now we'll just leave both versions in
4:25
function-based and class-based. Now to test it out, let's move this into place for now. And we need to update the URLs to point to the new views.
4:50
And we need to use the as_views method. And the rest should be the same.
5:08
Let's try this out. Typo here. That's okay. And a quote
5:44
and that didn't work yet because the class-based view expects a specific query set so I have to go to templates
6:01
list, and here it expects a quote_list because in views I only had to type model=Quote unlike the function-based view. I didn't return like a quote's
6:20
query list object. So class-based view works with model name underscore list so quote_list. After having fixed that, it should work.
6:39
And it works. I can edit it. That works too. And that confirms that the view works as well as the update view. And by the way
6:51
I found out that we can shorten this a bit further by writing and importing it.
7:13
I think this is a bit more elegant. Try that again.
7:31
The view works and the edit works too. And this doesn't work yet because I need to rename my template.
7:48
So here we have quote_delete, quote_detail, quote_form, quote list. But Django expects in a class-based views actually a quote confirm delete.
8:03
So I have to just rename this to be quote confirm delete.
8:21
And with that change all the magic of confirming the delete now works. Awesome! Great!
8:47
So as you see, class-based views are pretty elegant. It only takes a few lines of code, really to build a fully cracked application
8:56
assuming that you have your model and your form set up correctly. The template was just the same as the class-based view.
9:03
You only had to make a little tweak to expect a Quote on the script list and the list view and a rename
9:10
to work with a quote confirm delete HTML template. And lastly we had to update our URLs to call those class-based views with the class view method.
9:26
And for the rest, was pretty similar to the function-based but we saved a lot of repetition and code. In the last video we do the last step
9:35
and that's editing the model to our admin back end.