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