Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Build Routes

In this lesson, you’ll build out the routes for your projects app and review what you’ve already seen on the topic. Earlier, you went to the main file inside your management app (urls.py).

To that file, you added path('projects/', include('projects.urls')). Now, when someone types projects/ in the URL, they’ll be directed towards urls.py inside the projects app.

00:00 In this video, we’ll build out the routes for our projects app, and since we already did that—most of it—before, it’s just going to mostly be a revisit.

00:12 So, remember what we did at the beginning? We went to the main file inside of our management app, urls.py, and we added this include here. We said, “Okay, if someone types in /projects/ up in the URLs, we want to direct the person forward to urls.py inside of the projects app.

00:36 Step one. So then, we created this file over here and inside of this file, we mimicked what we had in the other one. django.urls import path, we did that. We created a urlpatterns variable, and added a path(), and what we did here is we directed it forward to the views, inside of here. To be able to do this, we needed to import those views first.

01:04 So, from projects we imported views, and here’s something I want to show you. You might see it around that people use . (dot), and . in that case just means the current project. So you could also write from . import views but I’m going to stay more verbose just to make it clearer.

01:21 So, from this app—from projects—I’m importing the views file, and then I’m able to direct forward. With the empty path I’m going to go to views.project_list.

01:33 What that allowed us to do is we got this. It’s directing forwards to a template. We still have that sitting up from the app that we built before. Let’s make a little change just to solidify this understanding of URLs, and of routes, and then paths. What I’m going to do in here, now, I will say—I’m going to add this here, 'test'.

01:59 So, I still want to keep this page around, but I want it not to happen at 'projects/', which we defined up here, you see? When it ends with 'projects/' and then nothing more, it would take us there, before. Now, I changed it so it takes us there if it ends with 'projects/', then 'test'. Let’s take a look at this.

02:19 If I reload this page, there’s nothing, because I just changed that. However, if I go to /projects/test, we can see that page from before. So, I changed the path. That’s what I did in here. Cool.

02:34 So, for /projects/, just the plain old one, without test at the end, we want to add a view where we can see all the projects that are currently added to our database.

02:45 Do you remember how this looked like in the finished app? We have this image and then descriptions down here. Let’s go ahead and set that up. So, I’m going to add another path() that I’m going to keep empty so that it’s just /projects/ and I will redirect to views.—let’s call it…

03:07 So, we’ll call this new view all_projects,

03:14 and we can see PyCharm marks this as brown, which is just indicating to us that it can’t find this, because it doesn’t exist yet. So we’re going to go on a visit with our friends again.

03:25 We head over here, delete this. This site can’t be reached and we’re getting a chance to say hi to the AttributeError.

03:37 Again, it tells us 'project.views' has no attribute 'all_projects'. So, this view function that I’m referencing to here does not exist yet, and Django’s nice and tells us about it. Great!

03:50 So that was a quick revisit of the paths and routes. I’m going to get rid of this one now because we don’t really need to keep it around. We’re going to go ahead through the process of building out the views and the templates, very similar to how we did at the beginning in the trial lab.

04:08 See you in the next video!

Become a Member to join the conversation.