Hello Django3:09 with Kenneth Love
With Django installed and a project up and running, it's time to make our first, Hello World view.
HttpReponse is the class that represents an HTTP response back to the client. The way we used it in this video is the absolute simplest way of generating a response. It's not the most useful tool in the shed, but it's the gateway to all of the other HTTP tools we have.
Django is an MVC, or model-view-controller framework, but Django doesn't call 0:00 templates views or the functions that return rendered templates controllers. 0:05 Instead, Django calls templates templates, and 0:09 the functions that return rendered templates views. 0:12 This can be a little bit confusing, 0:15 but you get used to this nomenclature really quickly. 0:17 It you've already done my Flask courses, you're probably used to it already. 0:19 So, to get our very own Hello World, we need to create a new view and 0:23 give it a URL. 0:26 Are your ready? 0:27 If we look in our learning site Stub, this little thing right here, 0:28 we see that we have this settings.py file, and let's look in there. 0:33 It's just a bunch of stuff you can look at, nothing super important just yet. 0:38 We'll wanna change some things later. 0:42 Anyway, though, inside of here we don't have a views.py file. 0:44 Or, if you've done a lot of Flask you might be looking for an app.py, 0:49 something like that. 0:52 We don't have anything like that. 0:53 All we have are settings, URLs, and our WSGI, our server interface. 0:54 The little Stub doesn't have a whole lot of functionality for our project. 1:00 But, this is really the easiest place for us to add just a simple view. 1:05 So, let’s add a views.py file in here to hold our view. 1:10 So we’ll do views.py. 1:15 There’s nothing in there. 1:17 We need to do an import and then create a function. 1:20 So the import we need to do from django.http import HttpResponse. 1:22 And then, we're gonna do a def hello_world and it's going 1:30 to take a request or an argument that we're going to call a request, 1:35 and it's going to return the HttpResponse that we imported. 1:39 Capital letters let you know that it's a class. 1:44 And we're just gonna return the string hello_world. 1:46 So we're gonna save that. 1:50 So, HttpResponse that comes from Jenga's http library is a class that lets us make 1:52 well, http responses. 1:57 The response is a really, very malleable. 2:00 Most of the time, 2:03 though, you're gonna have more specific responses that you want to send back, so 2:04 you're gonna send back specifically a certain error, or a redirect, or whatever. 2:07 But, for right now this is all we need. 2:12 So, we have our function that takes a single argument called request. 2:14 Call it whatever you want but calling it request is a pretty common standard. 2:18 It's a good idea to do that because that's what it is. 2:22 It's the request. 2:24 It's what the client has sent to your server that's made it all the way to 2:25 Django We don't need it for this view. 2:30 We're not gonna do anything with the request. 2:32 But all views have to accept the request, so we have to put it in there. 2:35 Most of the time, you'll likely end up using it one way or the other. 2:39 Finally, in our view, we create an return our HttpResponse. 2:43 And we've given it a string as an argument. 2:47 This'll put that string as the body of the response. 2:49 Okay, let's go see this live. 2:53 Wait a minute, we have to do one more thing. 2:55 We have to give it a URL route. 2:57 I know that seems like a lot of work for 2:59 nothing, but this a process that we'll be doing a lot in this course and 3:01 that you'll do repeatedly in your future work with Django. 3:04 So I think it's worth spending some extra time on, don't you? 3:06
You need to sign up for Treehouse in order to download course files.Sign up