Welcome back, everyone, to this lecture on using Django tags and URL names.
Recall that we mentioned in the path function in the URLs, that pie file, we can actually assign names
to specific web pages in our Django project.
This, in turn, allows us to use the URL tag to easily create links to other pages based on their URL
name in URLs, not pie.
So now you don't need to worry about having to memorize the exact file path to any particular template.
You can easily refer to them by the URL name.
This is a hugely convenient tool and why we want to actually name our URLs inside the path function
in the future.
Let's explore how this works.
OK, here we are back at the each HTML templates are working at earlier.
I'm going to just clear this if tag and focus on the fact that we have two templates.
One is the example each smell template and we can just go ahead and say template.
Save that and then we have variable each HTML.
Let's quickly review what their URL paths are.
So if we go to URLs dot py, this is right now under my site to remember we've connected my app to include
my app thought URLs.
So we're going to go then to your all pie, which is inside my app and I have these two paths.
One is the forward slash my app that goes straight to this example of you, which in turn in views.
Recall this connects to example of you render example that each HTML.
Then we have the variable path, which goes to variable view, which is just this variable here being
sent.
But the clear things and make it simpler.
I'm going to delete this and we're really just focusing on there's not going to be any context past.
But how do I connect this view to this view on the template side of things?
Not a redirect.
And so the view.
But actually, it's the button that says, OK, you're an example of you go ahead and visit variable
view, or at least our URLs.
So we're going to do here is an R refresh my website.
And if I just go to force my app.
I have example, HTML template.
Then if I go to forward slash.
Variable that I have variable Timol, what I would like to do is to have a link somewhere on this site,
or maybe I click some text here that links me to the variable.
Dot h HTML template.
So how can I do that using your URL names?
Well.
What I could do is the following I come to you URLs that pie, and my first step is to actually register
with jingo.
This app name.
And typically this is going to match the app name of the folder.
You decided just to make sure that it doesn't get too confusing.
So we come back here and we say app underscore name is equal to and the string my underscore app.
This is just the general notation.
So typically by convention, you're going to have the string match your actual application name so they
will go ahead and save that change.
Keep in mind, app name this is actually a special variable jingle looks for inside of your octopi.
You have to call it app underscore name.
OK, so we have the app underscore name variable, and we've now defined it inside URLs that pie.
And this is essentially going to register the app namespace.
Which is going to be used for the URL names.
So that's step one, the find the app name inside of URLs that pie.
Let's now move on to step two, which is actually give these paths names that we can use.
So, for example, I can give this path the name example because I salute the example of you.
And then I can give this path the name.
And let's just call it.
Variable, and I'm going to save those two, so I have to URL paths, one is example, one is variable
and they are located underneath the URLs folder, which has been registered to have the app name.
My app.
Now the question is how we actually use these names and inform Django on the template side of things
that they connect to a particular view.
Well, I'm going to come back to example the HTML, and I'm going to create an A. tag that actually
links to this.
So underneath that example is HTML template.
I'm going to create a new heading.
And let's call it the following let's make an anchor tag here.
And then we're going to say each ref is equal to.
And then inside a double quote string.
Typically, when you'd be creating this sort of reference, it'd be kind of annoying because we would
have to say something like C four slash users, Florida, slash my username and Django, et cetera,
all the way until I got to something like.
Variable HTML now, clearly, we don't want to have to write something as complicated as this.
So instead of writing all that, we end up using a URL tag, so we say curly braces.
Percent, nine percent sign curly braces and then inside of here, I use the key word.
You are L.
Space.
And then inside here, I'm going to have some strings that actually connect to the URL name.
And the first thing that goes in is the application name my underscore app.
And then you say colon and whatever your view you're actually interested in.
So right now, this is the example that HTML and I would like to have a button here that says, go ahead
and click on me and go to variable that HTML so that your name is called variable.
So we'll come back here and say.
Variable and then we do have to have the closing of that anchor tag and then some sort of button, so
we'll say click me to go to.
Variable and then we will close off that increase that.
OK, so pay close attention to what's happening here.
I have inside heading one.
This anchor tag and the ref for the anchor tag instead of being some long, complicated pathway to a
specific HTML file.
I'm now taking advantage of two things.
One is a URL tag and the two is the capability to actually then use the URL names, as well as the newly
defined app name to easily connect this.
And then I can reference this sort of thing anywhere on my website.
So I'm going to save that.
And let's just see what happens right now if we try to run this as it is.
So I'm going to come back here and, ah, refresh the page and it says, Hey, click me to go to variable
so we can see the anchor text there if I click this.
Now it takes me to variable thought HTML.
So what's happening here is jingo is now alerting it to the fact that underneath my app, go to that
URLs that pie and then search for the URL path that has the name variable and then serve that view.
So now you can see why it's so useful to begin naming inside your old patterns the different view path
connections.
That way, when you're dealing with the templates, it becomes super easy to reference things without
having to remember where exactly they are located.
Now, another thing to keep in mind is let's say you had some templates or some views or URLs that were
not associated with any particular application.
So if I open up explorer, really what I'm talking about is if for some reason you have your URLs on
a site level that you actually want to reference if you want to do that.
For example, you have some path here that leads to maybe your home page for your entire website, and
it's actually not associated with any particular extension.
All you need to do is if you have some views that are at my site level instead of at a particular application
level, you would just come back to here and remove my app and say something like variable.
This would be if the variable existed as a template on a project site level, but typically it's going
to be a lot easier to organize things by application.
And you can even organize your home page, so to say no extension based off some other app within your
project.
So what you mean by that is if you were to come here to your URLs, you would just define some pathway
here that was actually blank.
And then you would include something like home that URL.
So you'd actually end up just making a new Django application for all your kind of home based stuff
and simply connect it here.
It's kind of up to you.
Some people like to do it at a project level.
Some people like separate into kind of some homepage application.
It's kind of up to you.
But again, what's the main thing we learned right now?
The fact that all I need to do is step one give my application URLs, stop, file a registered app namespace,
then create a name for a particular URL pattern path connected to a view.
And that means in one of these templates, I can reference it and you don't need to reference it specifically
to make or tag.
But this is the most common way to do it.
Where you use the URL tag my app variable and this tag by itself is essentially OK.
Here's a direct link to go to this URL connected to this view.
So super, super useful.
We're going to be using this a lot, especially on something like a navigation bar at the top of the
page.
Speaking of which, let's go ahead and learn how to inherits from other HTML templates.
I'll see you at the next lecture.