Locked lesson.
About this lesson
To create a web page with Django, there are three important pieces: the view, the template file, and the URL. In this video, we'll cover views and how to create them.
Exercise files
Download this lesson’s related exercise files.
Django Views.docx57.1 KB Django Views - Solution.docx
57.6 KB
Quick reference
Django Views
To create a webpage with Django, we need three things: a view, a template file, and a URL.
When to use
Whenever you want to create a webpage, you'll need to create a view for it.
Instructions
To create a home view in the views.py file:
def home(request):
return render(request, ‘home.html’, {})
Hints & tips
- Webpage views go in the views.py file
- Remember that python is tab sensitive (don't tab with spaces! use the tab key!)
- 00:04 Okay, so we've created our first app, the resume app, but
- 00:08 when we hit reload here, nothing has really changed.
- 00:11 We still have this little rocket ship that's kind of shaking around, and
- 00:15 this is obviously not what we want.
- 00:17 So in this video, I'm going to start to talk about views.
- 00:21 And views are the first step in changing this screen right here in building
- 00:25 any sort of web page with Django.
- 00:27 So with Django, to create a web page, there's basically three things.
- 00:33 There's a view, there's a template file, and there's a URL, and
- 00:37 the URL is this thing up here, right?
- 00:39 So remember, we looked at /admin.
- 00:43 This /admin is a URL, right?
- 00:47 So, we need three things.
- 00:49 We need a URL, we need a view, and we need a template.
- 00:51 So in this video, we're going to start to look at views.
- 00:53 So let's head back over to our Sublime Text Editor and come down here,
- 00:58 I'm going to close this, and looking at our resume directory and then come down
- 01:03 here to views.py and we're going to be working in views.py an awful lot.
- 01:07 Anytime you really want to do something sort of a behind the scenes, complicated
- 01:12 with Django and a website, you're going to do it in the views.py file.
- 01:16 And the view.py file is sort of like the brains behind the scene.
- 01:19 It kind of does technical things for your webpage.
- 01:22 The template file, which we'll talk about in the next video,
- 01:25 just has the basic HTML of your webpage.
- 01:28 It's not doing anything all that interesting, but
- 01:30 the views.py file can do interesting things.
- 01:33 And we'll talk about what that means more in the next few videos, but
- 01:36 you'll notice there's some stuff in here already, and this is a comment.
- 01:40 If you're not familiar with Python comments out things with the hashtag,
- 01:45 the number sign, and that just means this doesn't get executed.
- 01:49 So we can go ahead and delete that, we don't really need that.
- 01:51 And you'll notice up here at the top it says from Django shortcuts import render.
- 01:55 And render is important, it allows us to render webpages, right?
- 01:59 So let's start to build out our first web page.
- 02:02 So we want a home page.
- 02:03 So let's create a home function.
- 02:06 So to create a function in Python, we type def to define it.
- 02:09 We want to define home and we want to pass in request, And then put a colon.
- 02:17 So, hit Enter after the colon, and you'll notice that the icon comes down and
- 02:23 it's been indented in, it's been tabbed in.
- 02:26 And now some people think that this is a bunch of spaces.
- 02:30 So I'm hitting the space key on my keyboard.
- 02:33 That's not correct.
- 02:35 This is an actual tab.
- 02:36 So if you hit the Tab key on your keyboard, it will tab this over.
- 02:40 And that's actually very important.
- 02:42 Python is tab sensitive.
- 02:44 So all of these things have to be tabbed correctly.
- 02:46 So do not use your spacebar in order to tab things over in Python ever,
- 02:50 you always want to use your Tab key.
- 02:52 So inside of here, we can return some stuff, okay?
- 02:57 And we'll talk about that in just a second.
- 02:59 First, I want to talk about this.
- 03:01 What is this request thing?
- 03:02 Well, anytime you've got a website in a web browser situation,
- 03:08 what's happening is somebody is requesting that web page.
- 03:12 So when they go to a URL, for instance local host 8000,
- 03:16 that's sending a request to the backend.
- 03:19 And this is our backend, right?
- 03:22 So we want to pass that request into this function.
- 03:25 So somebody is saying, hey, give me the home page, I'm requesting it.
- 03:29 So we pass that request into this function, and
- 03:33 we return, we type in render because we want to render a web page, and
- 03:38 renders a function, and inside of there, we want to pass that request.
- 03:43 So we type request, comma, and then quotation marks.
- 03:47 And these can be either double quotation marks or single quotation marks.
- 03:50 I'm going to use single.
- 03:52 We want to say where do we want to send this request?
- 03:54 Well, we want to send it to home.html.
- 03:57 Now we haven't created home.html yet, but we will soon.
- 04:00 And you want to put in another comma, and then you want to put an opening and
- 04:03 closing squiggly brackets.
- 04:05 And what these brackets are, is something called a context variable.
- 04:09 And this allows us to pass things into our webpage, and
- 04:11 we'll talk about that later on.
- 04:13 But for now, we can just leave this blank with an opening and
- 04:16 a closing squiggly tag.
- 04:17 So this is really all there is to it at its minimum,
- 04:20 this is really all you need to create at least a view for a web page.
- 04:25 And if we want to create another web page, for instance and about page,
- 04:30 we could just copy this whole thing and just rename it something else.
- 04:34 So if we want to call it about,
- 04:37 if we want that to point to about.html, we can do that.
- 04:42 I'm going to go ahead and delete this for now, just to keep things simple.
- 04:44 We'll probably add that back in later.
- 04:46 But that's as easy as it is to create new webpages.
- 04:49 So like I said at the beginning of this video,
- 04:51 creating a web page is really a three step process, you have to have a view,
- 04:55 you have to have a template, and you have to have a URL.
- 04:58 So in the next video, we'll go ahead and create a template,
- 05:01 I'll show you how to do that.
- 05:03 And that'll be in the next video.
Lesson notes are only available for subscribers.