Locked lesson.
About this lesson
Creating links in Django is similar to how you create them in HTML. However, in this lesson, we'll discuss the differences, and how to create them within your project.
Exercise files
Download this lesson’s related exercise files.
Creating Django Links.docx57.2 KB Creating Django Links - Solution.docx
57.7 KB
Quick reference
Creating Django Links
Django links are better than regular HTML links.
When to use
You should use Django links instead of regular HTML links every time you add a link to a webpage.
Instructions
To create a Django link to the home page:
<a href="{% url 'home' %}">Home</a>
Hints & tips
- <a href="{% url 'home' %}">Home</a>
- The name of each link comes from the name given in the urls.py file.
- 00:04 Okay, in this video, I want to talk about links.
- 00:06 And if you're familiar with HTML, you know how to create a simple link for
- 00:09 a web page, we're going to go through it.
- 00:11 But Django has sort of different types of links, and
- 00:14 they handle links a little bit differently.
- 00:17 And that's what I want to talk about in this video.
- 00:19 So, let's head over to our base.html file and instead of having
- 00:23 new header stuff just written there, let's create a couple of links.
- 00:28 So I'm going to create a couple of links and
- 00:31 these are just normal HTML links, right?
- 00:34 So it's a href.
- 00:40 Let's go about.
- 00:43 And for this one, we can just put a /, and for
- 00:48 this one we can put about.html.
- 00:51 Now, this points to our home page, which is just the root directory,
- 00:55 which is what this slashes and this one goes to about.html.
- 00:59 And if we look in our urls.py file here,
- 01:03 that's just this and this, think of this as that,
- 01:08 right, with a /, but for here, it has nothing.
- 01:12 So, okay, so let's go ahead and save this and
- 01:15 head back over to our website and hit reload.
- 01:18 And when we do, we see these two links.
- 01:21 Notice I put a little bar between them, that was just this thing right here,
- 01:26 just for aesthetic purposes.
- 01:28 But now we can click on each of these and this one goes to the about page, right,
- 01:32 this is my about page.
- 01:33 And we click this one and it goes back to our homepage,
- 01:36 you notice just the same homepage URL.
- 01:38 Now, this works, right, you can do regular HTML links with Django and
- 01:42 works perfectly fine.
- 01:44 But, django has a better way to do it.
- 01:47 And that's what I really want to show you in this video.
- 01:50 So to use a Django link, it's really, really easy.
- 01:53 We use these same open and closing tags,
- 01:57 but we do it inside of our href tag, right?
- 02:01 So, we just go, Like this, so we have an open tag, and a close tag and
- 02:07 we're going to talk more about these tags starting in the next video.
- 02:11 I've just sort of shown them to you but
- 02:13 haven't really discussed them in a great detail.
- 02:15 We'll talk about them in the next video.
- 02:17 And to use a Django link, you just type in URL,
- 02:21 because we want a URL and then we name it.
- 02:24 So this one is Home.
- 02:25 So where do I get this name?
- 02:28 Or remember back in our urls.py file, we named each of these links.
- 02:33 So you name= home, name= about.
- 02:36 Well, that is the name you put in right here.
- 02:39 So very cool.
- 02:39 So let's come down here to the about page.
- 02:41 Let's do it again here.
- 02:43 And we just want this, want a url, this one is about.
- 02:48 And again, go ahead and save this.
- 02:50 Again, we get this about from our urls.py file the name of this link is about.
- 02:55 If we call this about me,
- 02:59 then this would be about me.
- 03:03 But we didn't do that.
- 03:05 So let's go back here.
- 03:06 Make sure these are both saved.
- 03:08 All right, so now we can come back over here to our web browser and refresh it.
- 03:13 And, you'll notice nothing has changed, really.
- 03:18 These links look the same, they act the same, but now these are Django links.
- 03:22 And in fact, if we view the page source, right click in View Page Source,
- 03:27 we can see these links look exactly like they did when we just created
- 03:32 regular HTML links at the beginning of this video.
- 03:36 That's because Django will convert these tags into links.
- 03:40 Okay, so it kind of begs the question, why do it this way?
- 03:44 It seems like more work to have to enter all these things.
- 03:47 Well, it goes back to what I was talking about with the base study HTML file,
- 03:51 making one change that reflects across your entire website.
- 03:55 So in the future, your links may change, right?
- 03:59 There's a problem with legal department.
- 04:00 They say you can't say about anymore, now it has to be About Me, right?
- 04:05 And so, this has to go to something else and blah, blah, blah, about me.
- 04:10 Well, if you've used HTML links, you have to come in here and
- 04:15 everywhere, you have to update these things, right?
- 04:19 Well, if we've done it this way with Django links,
- 04:22 we could just update this one time.
- 04:24 So instead of about, let's say we want this to now go to about -me.
- 04:31 Right, the actual path of the URL,
- 04:34 we can just make that change right here and save it.
- 04:37 We don't have to make any changes here or anywhere else in our website.
- 04:40 If we have links other places in pages of on our website, we could have hundreds of
- 04:45 links scattered around in the content throughout the website.
- 04:49 We'd have to go through there and change every one of them.
- 04:51 But now, we just make that one change, we can come back here and
- 04:55 hit reload and now when we click About, it goes to aboutme.html, right?
- 05:00 Which looks like a whole new page, but it's not we just changed the URL.
- 05:04 So, that's the reason why you want to use these Django links.
- 05:07 It's just very easy to update them in the future and super important.
- 05:12 I'm actually going to come back here to our urls.py file and
- 05:15 change this just back to about, like it's a little cleaner.
- 05:19 So very cool.
- 05:20 So that's all for links.
- 05:21 Links are very simple.
- 05:22 In the next video, I want to start to talk about these tags and
- 05:26 really kind of get in more depth about them.
Lesson notes are only available for subscribers.