2.4 Our First View
If you have never worked with Django before, this terminology might be a little confusing for you. In many other web frameworks, a "view" is the part of the application that uses HTML to present data to the user. In Django, this isn't the case. In this lesson, I'll explain how a Django view is the part of the application that handles most of the logic for application functionality, similar to the "controllers" of other MVC-style frameworks.
1.Introduction3 lessons, 14:42
2.Foundations6 lessons, 47:44
3.Add Structure6 lessons, 52:54
4.Clean Up3 lessons, 16:58
5.Conclusion1 lesson, 01:23
2.4 Our First View
In the previous lesson, we got a little bit further in the process of creating a URL that's going to ultimately mapped to a location within our application. Where we can start the process of presenting some information to the end user. And the reason I say start the process of presenting, is because if you are familiar with other frameworks, when you start talking about model view controller and things of that nature, when you start to talk about views, you're starting to talk about actually presenting data to the end user. And this is where Django starts to skew its path, just a little bit, in that views aren't really where you're gonna present information to the end user. A view in the world of Django is where you're going to put the logic of your application. When these requests come in to specific URLs, if I were to switch back over to my text editor like we created here, so at this point we're already into the news section of our URL, and now I want to take the information or take this path and funnel it views.articles_list. And this is what we're referring to as our view. Now interestingly enough in World of Djanga, when it comes to views we're talking about functions and we're talking about functions that live within our views file and that's what this particular syntax is talking about. So let's start to get a little bit closer to our end goal of being able to present something to the end user, not necessarily our data but we'll get there. So if we were to take a look at this structure here, we see we have views.articleslist and typically when you see a structure like this, this first word here oftentimes when you're talking in Django is referring to a file. And in this case, it's exactly what it's referring to here is views.py. Now within Then here also I'm saying I want to use articles underscore list. So if I look into views nothing like that exists yet and that's exactly what we need to do, we need to create that. In order to create that view we're actually defining a function. So we're gonna come in here and say we're going to define articles list. And then within here this is going to take a parameter which is going to be the request. So we get ahold of an object that is going to contain all the information about the request and that's gonna come in handy a little bit later as we start to try to present meaningful data to the end user, but we'll get there. And all we really need to do at this point is we're going to use this render function that we are importing. So we're going to say render, and this is going to take a couple of different parameters. The first parameter is going to be the request. So we're gonna forward on the request that we get ahold of within this view. Secondly, we are going to specify what's known as a template. And in the world of Django, a template is more akin to views, say in other mvc style patterns. So it's actually going to be that HTML file that we're going to start to use to present data to the end user. Now we haven't created this yet but we're just going to specify in quotes here the location of where that's going to be found, and we're gonna say, within news/articles_list.html. Now this is just a convention to use the same name as the function, or as the view. You don't necessarily have to, it could be a different name here if you would like, but that's just, like I said, somewhat of a invention. And then the last piece here, we're just going to leave as open and closed curly brackets for now. And you'll see later on that this is a way that we're going to be able to kind of start to glue together data with our view, but once again, we'll get to that a little bit later. So once I've done this I can go ahead and hit save. And if I were to come back to my web browser and I refresh this, you're gonna see here it has changed a little bit. Now we've gone from we can't find articles_list, to a template that does not exist so we're furthering down that list. Now the nice thing about Django is when it presents these errors to you in a development environment it's pretty good about telling you where things are happening directly. And if you start to look down, you see information about the request method, request URL, Django version, exception type and exception value. So right here it's saying that it cannot find a template. And this template that it can't find is news/articles_list.html. So that's the problem that we've run into. And if you recall, within our text editor here, that's exactly what I'm specifying here in this render function. So I'm saying I want to use this template, but of course, at this point, that template doesn't exist. So that's what we're gonna start to take care of in the next lesson.