Learn Django Class-Based Views - Using TemplateView - theory and examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another django tutorial so this is the start of a new mini theory series of videos covering the theory of class-based views in django so i intend to in this series take you through step by step and have a look at each of the different class based views that are built into django go through some of the theory and give you an idea of how and why you might want to use that particular view in this tutorial we focus on the first of the django generic views template view so let's answer the question what are class based views in django a view is described as a callable so it takes in a request and it returns a response so generally we use a url to connect and direct requests to the views so you've probably seen in django there's two approaches to creating views a function based approach and a class-based approach so there's probably no right or wrong way to work here whether you want to use function-based views or class-based views so really we're asking ourselves the question how do we want to develop our application so using class-based views it potentially provides us some additional benefits over utilizing function-based views generalizing the benefits of class-based views class-based views allows us or promotes code reuse through inheritance and mixins so it allows us to better follow potentially the dry principles of reducing code duplication these features all together potentially then allows us to better structure our code class-based views have been known to cause some challenges for new users to django with functions you generally have all the code up front you're in charge and control of the code and you can see it and it's in front of you with class based views and particularly utilizing django's generic views the generic class-based views built into django inherit methods and attributes from other views this does mean generally that we're writing less code than if we are using function based views but it does provide a level of abstraction by this i mean for new users you're not necessarily seeing all of the work that's being done because it's been abstracted to these other inherited methods and attributes so some people refer to this as the magic and suggest that django has too much magic in these uh class-based views that are available in django so the whole point of really of this series is to take the magic out of class-based views or the generic class based views and give you an overview of all the different generic class-based views that django has to offer so by now i've established that django has class-based views pre-written code available for us to create our views using class-based views so it's these generic views i'm going to be focusing on in the first couple of tutorials in this series you can see here that we can access in our view the generic views or class based views via the django views generic starting from what is probably the most basic class based view that we can build the template view so i'm going to give you some examples in a minute or an example of using the template view but let's just start by saying when might you use a template view or the template view so this is a generic view and generally used to show static pages or alternatively pages that use get requests so this is your maybe bulk standard page static page that you've created for example an about us page that maybe doesn't take any data from a database just a static page although we can do that but nothing else is on that page potentially where users interact and maybe add new data to the database and send requests back so generally you're not going to be using the template generic template view for showing a page with a form on the page or maybe creating or updating data on the database and this is simply because there are better options so now let's have a look at how we actually implement the template view first up we're going to be utilizing the template view within the url configuration so in actual fact we don't actually need to build a view to utilize this class based view the code for this tutorial you can download this code and follow along so this is an example of using template view within a url so here for example we have an application called cbv and we have the main core application in core so that's the current setup we got here but you can see here we're going to be importing from generics the template view and then within the url we define the path as per normal and then we go ahead and define our view so here we say template view as view so here what we're doing is we're taking the template view and all the pre-written code that's within so all the methods and attributes within here and we're just utilizing it as it is so there's no difference here from creating our own view using a function this would just be calling that there but you need to remember here that these generic views are pre-written views so we're just hooking into that view and now we have the opportunity to override and add to the attributes inside of the parentheses here so there's two attributes that we can add into the parentheses of the view here so the first is the template name so this is basically as it sounds it's the template name the template that we're going to be utilizing so in your templates there's going to be a template called example ex1 and it's obviously going to send you there so when you go to the url this url here is going to take you to that template so in addition to that we have extra context so we can pass in extra context so we have to utilize the attribute extra context and there we can then add some context in this case we've got this key value so title and the value custom title so we can output a context or data directly from our url patterns here utilizing the template view so going back to this slide here it starts to make sense here so we have the class based view that we're utilizing template view we pass changes to class place view inside the as view and arguments passed in to the as view were override attributes set on the original class which is the template view class so remember that the template view class the built-in class is behind the scenes it is just like a view we'd normally create um as a function but is a class and so therefore it means that we're able to inherit the attributes and methods with inside the template view and override them to fully understand what is actually going on here we need to and well we need an understanding really of object orientated practices the idea of classes inheritance and such so if that doesn't make sense or if you're not familiar with object-orientated programming this is where things start to fall down a bit and i guess new users to django who don't necessarily also have a background in object orientated programming this is where the abstraction probably and difficulty understanding class based views may come into play so to give you an idea these classes here at the top are already pre-written in django here the template view and other views that we'll be covering in this series these are the class-based generic views that we use now this view here is inheriting all the methods and attributes from these classes here at the top so that allows us then to utilize all the functionality in these three elements here and what's in the template view to generate all the instructions that are needed to take in a user request and then to create a response and then send a response back to the user so the reality here is that you if you wanted to fully understand exactly what's going on here you would need to read up on these different views here and the template view and see what's happening inside so that you can have a better understanding of how you can then and what you can overwrite and actually do in this class based view here the template view so there isn't too much to understand in reality in terms of what you can do with this template view because it is really just a simplistic class-based view where we can take in a simple request and send a response typically just a url and maybe some context so let's have a look an example of using template view in an in a view in the view so here you can see the setup i haven't explained it fully apologies so we've just got the core application here with the settings and we've got this cbv application here and i've just set up some templates and some views and you can see here in the urls in the cbv urls i've set up an additional path now to ex2 example 2 and now we're going to create a view here utilizing the template generic template view so go inside of the view and you're going to see a few bits inside of here trying to left some instructions and some comments for you to understand what's happening here sometimes comments can kind of get in the way and just looks a bit like what's going on um so here you can see i've imported the template view now we've established that the template view is inheriting the template response mix in the context mixing and the view so that's all happening behind the scenes so what we can do now is i've created a simple database just with one field name and just to provide an example and you can see here now we create a new class and i've just called that class ex2 view example 2 view and we're connecting or inheriting or extending sorry from the template view okay so like it says here the template response mixing so this is the first thing that gets inherited or the template view um inherits from so it provides a mechanism to construct a template response given a suitable context so these are some of the attributes that we can pass into that the first one which is probably the main one is the template name so i'm just going to get rid of that to make it easy to understand what's going on so this is the first attribute that we can pass into this template view here or overwrite the attributes and here's a template name so obviously that's generating or connecting to our template which is inside of here the templates folder ex2 okay so that's our template and what you'll find from reading is that we can also pass in for example or define the template engine so maybe we're using a different engine ginger genshi and these are different templates engines so if you're not familiar with template engines django obviously has its own template engine that we utilize and we can override that and utilize other template engines so other attributes that we can change is the response class custom template loading or custom context object instantiation apologies instantiation sorry and then we have the content type so here we have the default django uh content type that's been utilized if we don't define this so that's the text html so this just these are here are just additional attributes that we can define generally to set up a simple template view we might just set the template name and that's then completed so now we want to potentially add some context or return some context so we can do that here you can see um by utilizing the content or context mixing so let's go back to our diagram so here we set up the template response mix in where we define or we override the attribute which is the template attribute so those well that's where we get all those attributes i've just shown you from from so now we're looking at the context mixing so this provides a whole load of different items that we can configure so one of them is the context so here we can define context so here we set up the context via get context data and now we start to kind of overwrite or add some context so you can see here that i've created a context called posts inside of that i've just looped through the database where i've just got one item from the database in this case i've made a query so now i've got all the data from the database inside of this context here and then the second item here just for an example of called data i've just added a string here so now we can return that back to the template okay so that's all achieved and managed remember via the content the pre-written context mixing that's inherited in so please do let me know if this is sinking in this makes sense to you because there's almost no point having this video if it doesn't make sense if that um what i'm saying um it doesn't you don't quite understand i'm not saying it in simplistic or in a way that you're connecting to then please let me know i will actually write something for you if you've got any questions etc i do actually want you to understand this this is why i'm making this video um this is not a financial move this is purely for learning so i do want you to be able to learn this content so please just let me know so let's just finish up here by just starting the server and should work okay so the code like i said is available um so again we've got the url set up we've got the models in place and we've got some templates so let's just see what this looks like so there's no surprises here you can see that we set up the two urls and let's go now and just go to ex1 so this was the first example where we passed in some data in this case we passed in the custom title so let's just go back to our url so you can see here and we passed in this extra content here called um well title so reference it via title so if i go into the ex1 html that's what we just saw you can see i'm referencing the title and any other data utilizing the doubles here and let's now have a look at number two so you can see here i've got data and post name so data was some context i passed in from the view so data should say context data for x2 and i've added some data to a database just one item i'm just extracting that out and putting into posts so let's remember that we return the data in post and then we need to drill down to actually get to the database data so in the models it's called name so this is why in our file here in the ex2 this is why i'm calling posts or referring to posts and then drilling down to the name so that's the exact data i want if i don't do that here i'm just returning the object back so if i refresh the page on ex2 example two you can see it just says post object one so to actually drill down to the data if you're not familiar with this then all you need to do is add in the data that you want so in this case the title and that's the specific data that i'm looking for and that didn't work because it's name not title so the name so this is referring to the name in the model the name that's what i'm looking for and there we go okay so if you've just written your first class base view then congratulations well done we're now going to cover more of the generic class place views inside django i'll go through each one and give you a little bit of explanation like i've done here and some more examples this is a fairly simple view to utilize in comparison to some of the other views that are available and the amount of different attributes and facilities that are available in those other views but hopefully you can see here the context of this class-based view and when you might want to use it it really is just a the standard or a standard class based view where we can just throw out a new template and maybe just return some data on the page so remember here that we're not taking data in so originally i said this isn't very good or isn't equipped for actually returning data in so why i said that and i said that there are there are other better placed class based views for that is because again if you look here in this what the template view is actually inheriting it doesn't necessarily support the it doesn't necessarily support the tools and the features that you're going to need to return data from the template and this is why again i suggest that this isn't necessarily the best class based view to utilize to um show a form or input or post to form data to this view okay so hopefully that's making sense and hopefully you're seeing how everything is starting to be generated or how everything is um formulated in django with these generic class based views so thank you very much for listening hopefully you got to the end if you did like i said if you've got any comments or questions sorry just let me know if you feel as i can improve something here um also let me know i'm happy to make a version two of this not a problem so here i know i'm carrying on but really try and focus on this idea here this is the main part that i wanted you to kind of uh start to scaffold in your mind so that we can work on other class-based views the generic views that are built in because once you understand what's happening here your understanding of why and when you might want to use a particular class based view and obviously once you get better at understanding what the class based views do the generic class based views we'll have a much better understanding um of when we can use them and also what you want going to use them for i realize i'm just repeating myself now um and also we can start to then build our own class-based views
Info
Channel: Very Academy
Views: 13,498
Rating: 4.9902911 out of 5
Keywords: class based views, django class based views, django view, django createview, django generic views, django detailview, createview django, django view class, django cbv, django class based view, templateview django, generic views in django, django, django tutorial, djangotutorial, django tut, django beginners
Id: GxA2I-n8NR8
Channel Id: undefined
Length: 20min 35sec (1235 seconds)
Published: Wed Aug 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.