Build Your Routes
In this lesson, you’re going to look at how to build routes. In the previous lesson, you tried a URL that doesn’t exist and got an error message that told you to check
When you check that file, you can see all of the URL patterns that exist there. If you try to go to
/projects, then you’ll see that it isn’t registered anywhere, so Django doesn’t know what to do with it. Fortunately, Django provides a lot of useful information in the comment at the top of that file.
Let’s get ready building our routes. So remember, we tried this
/projects/ up here, we tried a URL that doesn’t yet exist, and we got this error message telling us, “Okay, okay. This is not working. Can’t find anything here, but why don’t you go ahead and check in
Maybe you should do something there.” So, over here in
portfolio.urls, I’m taking a look and it tells me all the
urlpatterns that currently exist is one path to
/admin, and that directs forward to somewhere else.
00:53 One thing that Django’s great at is—not only the error messages—but also just information that’s included when you start a new app or a new project. Really, all you need to know is written up here in this multiline comment.
You see, the structure is very similar to the one that we already have. We can even go ahead and just copy this. I’m going to take this, that’s what Django suggests me to do, is make a new one:
and that’s it. Now,
include() is used, and we replicated exactly the suggestion that Django gave us up here. We made a new
path() and it’s pointing to the URL
projects/, and what we’re going to do next is we’re going to include
This is one of our new friends.
No module named 'projects.urls'. It’s kind of self-explanatory, but it’s still a bit scary to see it sitting around like that, so let’s try to pick it apart.
Django seems to be looking for a module named
projects.urls. That comes clearly from what we said here. We told it to include something that’s sitting inside of the
projects app, and then it’s called
.urls. Remember seeing that before, when it told us to look into
portfolio.urls in the error message?
So, as you see, it’s finding it, right? So what we created was good. Also, when you see a new friend, a new error message, that’s always a good sign because it tells you that what you did actually had an effect. So it’s telling us, “Okay, this
...does not appear to have any patterns in it.” Okay, so,
The included URLconf, which is a success message telling us we managed to include the right thing, but currently, it
...does not appear to have any patterns in it.
If you see valid patterns in the file
then the issue is probably caused by a circular import. We don’t really know what’s a circular import, but we can even see that Django gives us some tips here. However, that’s not the case for us right now.
Now, we’re not going to deal with this
'admin/', so I’m just going to get rid of it. And we don’t—actually, let me keep that. We don’t want to include, we don’t want to point forward to a different
urls file—that’s what we did here—so I get rid of that.
What I want to say is if I get redirected to here, to this
urls file, without anything else—you know, it’s just
projects/ and then nothing else in the URL—then what I want to do is I want to point forward to somewhere else.
Just cleaning up. I want to point into
views and then
project_list. That’s what I’m eventually going to want to create. Okay. So, here we have another error message that tells us
Unresolved reference 'views'. That is because if we want to use the
views inside of any app, we’re going to have to import it.
Become a Member to join the conversation.