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 lesson, we'll discuss how URLs work and how to create them within your project.
Exercise files
Download this lesson’s related exercise files.
URLs.docx57.3 KB URLs - Solution.docx
58.3 KB
Quick reference
URLs
Each web page of our app needs its own url. We define those urls in the urls.py file.
When to use
Anytime you create a webpage, you'll need to designate its url in your urls.py file.
Instructions
Modify your original urls.py file to this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('resume.urls')),
path('admin/', admin.site.urls),
]
Then create a new urls.py file inside of your resume directory. Modify it to this:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
]
Don't forget to add your resume app to your settings.py fil in the "Installed_Apps" section.
Hints & tips
- Your Django project should have two urls.py files
- One goes in your main directory, the other goes in your resume directory
- Every webpage of your site needs a URL that you designate in the urls.py file in your app folder.
- 00:04 Okay, so we've got our views, we've got our templates.
- 00:06 Now, we need a URL.
- 00:08 And if you look in our resume directory, there's no urls.py file and
- 00:13 that's kind of a problem.
- 00:14 So let's head over to our Django web directory.
- 00:17 If you look in here, there is a urls.py file.
- 00:20 So let's open that.
- 00:21 Now, I'm going to get rid of all of these comments, we don't need them.
- 00:26 Go ahead and save it.
- 00:27 Now, this is the main urls.py file for our project and
- 00:32 we need a separate urls.py file for our specific resume app, all right?
- 00:38 So that's what we're going to work on now.
- 00:41 So first things first, we need to tell our main project that our resume app exists.
- 00:48 So to do that, we go to our settings.py file and
- 00:50 scroll down till you see INSTALLED_APPS.
- 00:53 Remember, we created an app, we called it resume.
- 00:56 So we need to designate that right here.
- 00:57 So just come down here, hit Enter to go to the next line, put single quotation marks,
- 01:02 and then just type in resume, and then put a comma at the end.
- 01:06 Now, Ctrl+S to save this file or come up here to File, Save.
- 01:10 All right, so now our main Django website project knows
- 01:15 that a resume site or resume app exists.
- 01:18 So that's step one.
- 01:19 Next, we need to come down to our resume directory and right click and
- 01:24 create a new file.
- 01:26 When it pops up, let's go to File, Save As, and let's save this as urls.py.
- 01:33 So you can see, boom, it pops up right there.
- 01:35 So now, we need to tell our original urls.py file that's inside
- 01:41 the Django website directory that this new urls.py file exists.
- 01:46 And it seems a little complicated, but it's not too bad, and
- 01:50 we'll only have to do this once.
- 01:51 So what we do is, let's go ahead and
- 01:54 close all of these because this can be confusing.
- 01:59 So we want to come to our Django website urls.py file and
- 02:02 the first thing we want to do is add the include module.
- 02:06 So we type include up here.
- 02:08 So we want to import path and we want to import include.
- 02:12 And include allows us to include other files and that's exactly what
- 02:17 we want to do, we want to include this new urls.py file in our old urls.py file.
- 02:23 So we add include.
- 02:25 So now, we want to come down here to our urlpatterns and
- 02:29 hit Enter, and then tab over, hit the Tab key on your keyboard,
- 02:34 and we want to create a new path.
- 02:37 So we type in path and then parentheses and
- 02:41 single quotation marks and then the colon.
- 02:46 And now, the single quotation marks means our home page, which is localhost:8000.
- 02:53 It doesn't have a slash, anything after it.
- 02:56 Remember when we looked at the admin section, it had a slash admin next to it.
- 03:00 In fact, here's the URL for that right there.
- 03:02 We just want the home page to redirect to this new urls.py file.
- 03:08 So we put single quotation marks and then a comma and
- 03:13 then we want to include quotation marks,
- 03:18 single quotation marks, resume.urls, all right?
- 03:23 And that's what this file is here.
- 03:24 It's in resume.urls, all right?
- 03:30 So then, we want to put a comma after here and we're good to go.
- 03:35 So now, let's save this file, Ctrl+S, we come up to File, Save.
- 03:40 And next, we want to copy all of this stuff, just Ctrl+C or
- 03:44 you can right click and copy.
- 03:45 And we want to close this file and then head over to our new urls.py file that's
- 03:50 empty and you can right click and click Paste.
- 03:53 Now, we can get rid of this and we don't need to do include.
- 04:00 Now, let's come through here and get rid of those.
- 04:03 Okay, so this is our new urls.py file, go ahead and save this.
- 04:07 And from now, on every time we create a new URL for a new webpage in our app,
- 04:12 we'll do it here in this urls.py file.
- 04:15 To create a path for any webpage, we just come down here,
- 04:18 make sure you're tabbed over, do not space over.
- 04:21 You want to tab over, very important, and then just type in path.
- 04:26 And then, we want this to be the home page, so it's just empty, and then comma.
- 04:32 Now, point this to our views file.
- 04:35 So we type in views.home,
- 04:39 and then give it a name.
- 04:44 Name=, let's just call it home.
- 04:46 So now at the end, we want to put a comma.
- 04:49 So go ahead and save this.
- 04:50 Now, this views.home, this is referencing our views.py file and
- 04:55 our home function that we created a couple of videos ago, right?
- 05:00 So essentially, we're just pointing our home page or
- 05:03 home URL, which is nothing, to views.home and we've named it home.
- 05:08 Now, why do we have to name these things?
- 05:10 We'll talk about it later on when we look at links.
- 05:12 But for now, just realize every time you create a URL,
- 05:15 you just have to give it a name.
- 05:17 And it really doesn't matter what the name is, but we're creating a home page,
- 05:21 so we're going to call it home.
- 05:23 And it's going to point to our views.home,
- 05:26 which is views.home, which itself points to home.html.
- 05:32 So it's a three step process.
- 05:33 It's all kind of circular, it all references itself.
- 05:36 Now, we have to do one more thing to our urls.py file and
- 05:40 we have to import our views because right here, we're referencing views.
- 05:44 We haven't actually imported that.
- 05:45 So up here at the top, we just type in
- 05:49 from.import views and go ahead and save that.
- 05:54 Now, the period means from this directory.
- 05:56 So we're in our resume directory, we're saying from inside of this directory,
- 06:01 import views, which is just our views.py file.
- 06:05 So we go ahead and save it.
- 06:06 Now, if we come back to our app and hit Reload,
- 06:11 we see this is my home page Woohoo,
- 06:14 which if we come back here and look in our templates file and
- 06:19 our home page, is this exact thing right here.
- 06:24 So if you see this congratulations, we've created our first webpage and yeah,
- 06:28 that's really, really cool.
- 06:30 I know it's a little bit convoluted the three step process, the view,
- 06:34 the template, and the URL, but
- 06:36 you really only have to kind of wrap your brain around at once.
- 06:40 And after that,
- 06:40 it becomes very easy a as you'll see going forward when we add new pages.
- 06:44 So that's all for this video.
- 06:46 In the next video, we'll look at extending a base file.
- 06:48 I'll tell you what that's all about and that will be in the next video.
Lesson notes are only available for subscribers.