URL Linking: app_name, Path Names, and Arguments
In this lesson, you’ll learn about linking templates together, namespaces in Django, path names, and passing arguments in URLs. You’ll also make friends with a new error:
In this video, we’ll talk about linking templates together, we’ll talk about namespaces in Django, what’s the
app_name, how do you assign path names, and also how to pass arguments around in your URLs.
01:20 It’s a Django template tag that looks like this, and then something follows after here. I’m going to run it, for now, like this. We keep it empty because that’s the piece that we’re going to talk about a lot more. But at first, let’s see what happens when we run it like this.
I’m going to reload the
/projects/ page. And what we get now is a
NoReverseMatch error. It’s telling us that the
Reverse for—and this maybe looks like a double quote, but it’s actually two single quotes. You can see it down here.
So, we have two single quotes, which is just what I put—if you remember, I put
url and then these two single quotes. That’s just what it’s telling us here, that it couldn’t find a reverse for an empty string, essentially.
02:14 That it’s not a valid view function or a pattern name. We are going to focus on this pattern name. We’re going to figure out how to assign pattern names and how to deal with namespaces in Django in general.
NoReverseMatch is our new friend here, and I’m going to take this error as an example to dive a bit deeper into debugging errors, because it’s an error that comes up a lot and we want to practice some ways of like, “Where can we look?
02:48 We want to figure out what that is all about. And for that, we want to give names to patterns. The path that we just recently built, looked like this. Let’s take a look at what are the different names that we can give, that we can assign here to create a proper namespace.
So, here’s the path that we just recently built, and let’s take a look at how can we correctly link to this path coming from our template? I started off making this
url tag and then left this part here empty.
Now, what we can see here is it says
'app_name:name' and then we also have this other part here: the argument that we’re going to talk about later. First, we’re going to make a functioning one just with these two parts.
03:27 We’re going to talk about that because this is the namespace that helps us to directly get to the specific view that we’re pointing to. Our task now is to give a name to the patterns in here so that we’ll be able to address it like this inside of the URL.
And this is what’s going to come over here inside of the URL string, defining it. So, that’s the name of the app. First, the
app_name, and second, we’re going to need the name of the path, and we can add this as a keyword argument inside of the
path(), which is simply
name= and then we can give it any kind of name that we want to. Generally, I try to keep it the same as the view that this is pointing to. And, as you might guess, we’re going to pop that
name over here. So we have
:, and then the
name of the
path() that we’re directing to.
04:35 This uniquely identifies a Django view. That’s the namespace for our Django view, and if we put this into the URL, it’s going to know where to direct us to. Okay, let’s go ahead and fix that in our project and then see what happens next.
And with these two features, the
name and the
app_name, you can uniquely identify the views that we’re pointing to. So over here, in
all_projects.html, I want to now point to
05:59 Okay, so this should create a proper namespace and uniquely identify the view. Let’s give it a try if we can—oh. We can also see the server started again, so let’s give it a try whether everything is fine and whether we can use the button to get to the details page. All right.
So, after the reloading this I got, again, a reverse match error. I got another
NoReverseMatch error, but it is a different one. If you look here, it’s telling us not anymore that there is no valid pattern name or view function, but it tells us the reverse for
'project_detail' with no arguments was not found.
This is the important part here. It did find a reverse for
'project_detail', but not one that doesn’t require arguments. And then there’s some regular expressions here, some patterns that it tried.
06:51 It’s also pointing us to where the problem sits, down here. Always take a quick look at these error messages. They’re very useful. So, what we need to do is we need to pass an argument forward in order to be able to retrieve the right URL.
The way to do this is that inside of the template, additionally to the namespace consisting of
app_name and the
name of the path, separated by a space, we can put in a number, essentially, here.
07:29 We’re going to want that to be a different number, depending on what’s the resource that we want to access, but essentially it just has to be some kind of argument that gets passed forward to the function. Let’s take a look at this.
I can reload. There’s no errors coming up. And now when I press, this button—yay! We’re getting to a details view. The details view number
1, which is correct for this one. What happens when I press this one? All right.
So, I get redirected again to the number
1, because simply every time this URL—this button—gets clicked, we’re just passing
1 forward. That’s not exactly what we want, but we’re getting close, right?
To explain this again, this is the thing. Whatever comes in here is getting passed on to the browser to create the URL, and this is the URL that it’s going to retrieve and pass forward to the view function, which then decides based on that, which
Project to grab from the database and what to render.
And this is just going to be
1 for the first project,
2 for the second project, et cetera. So now, we’re passing in this number—that is relative to the project that we’re looking at—into the URL resolver here, so that then, it’s going to direct us forward to the appropriate details page.
Good job! That’s what we wanted to achieve, and now our functionality here already mirrors the functionality of our finished app! So, remember to thank
NoReverseMatch for that, because it’s really an error you might be encountering very often.
Become a Member to join the conversation.