Check out a free preview of the full Practical Guide to Python course:
The "Views, Routes, & Templates" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina explains how Django manages views and routes within the application. HTML templates are returned based on the pattern of the URL. Class-based views are also discussed in this segment.

Get Unlimited Access Now

Transcript from the "Views, Routes, & Templates" Lesson

[00:00:00]
>> Now, let's chat about views, routes, and templates. Views and routes allow us to map URLs in our web application to pages, API, and points and more. Our blog right now has three different sections. There's an index here that will show all of our posts. If I click on Read More, I go to a detail page.

[00:00:26] And I also have an about page right here that is at /about. So we have routes that are defined at the project level in practical_blog/urls.py and at the app level at blog/urls.py. Let's take a quick look at those side by side. So on the left hand side is the practical_blog URL configuration.

[00:00:54] This is the top level configuration for everything. Here, I'm including a path to the admin, which we'll look at in just a second. It's one of the built in things about Django that is really fantastic. The next thing is an include("blog.urls")). So that's going to take all of the URLs that I've defined in my app specific blog.urls and import them here.

[00:01:22] So looking at urls.py in blog this is where you'll see I've defined the index, the about page, and the detail page. And that is the path that I'm passing in as the first argument. We also have some templates, and those are located in the blog > templates/blog directory.

[00:01:52] So a bit of a weird data structure, but that is what Django expects. We also have static/blog/css. If I take a look at base.html, this is just the base template that all of my other templates will use. It's got a header and a footer and all of those reusable elements.

[00:02:18] I need to call load static at the top here, which means load up all of the static resources I might need, such as my CSS. These funky looking tags with the curly braces and the percent symbols are template tags. And those allow you to use some special commands in your html templates that let them interface with Django.

[00:02:43] Now, the important thing in base.html is this block right here. This content blocks lets us know that we can use base.html as a template. If I then take a look at about.html, I have a template directive here that says, extend this one from blog/base.html. And then everything that is in between these content blocks, insert that, Right into here, right into the matching content blocks.

[00:03:22] So that is pretty straightforward. We have a post_list that has a little bit of a more complex template tag usage. Here we have a for loop. We are getting a post list variable passed into this template from our view, which we'll look at in just a second. And we're looping over it.

[00:03:41] So for every post that we have, we're going to display it. And then for the detail, we are using a post variable. And we are pulling out the post title, the post.created_at | date and the post.body. The slug that is being used here for the Read More button.

[00:04:03] We're using this URL template tag, and we're passing in post, which is the name of the view followed by the slug. This matches our URL defined here in urls.py. So that lets us get a link to the detail view. Now, we're gonna take a quick look at views.

[00:04:34] These are some simple basic views that you might define when you're first starting out. But the more advanced concept is class based views. So here we have our defined views. And these views map to the URLs that we have defined in your blog urls.py. So here we see views.about, views.index, views.post.

[00:05:07] And we've also given these names so that we can access them from our templates. Now, this view, the about view, is just getting a template and rendering it and returning an http response. This index view is taking a post, getting all of the objects in the database via query, creating a context.

[00:05:34] So we remember in our template earlier, we used a variable called post_list. This is where it came from. We're passing it in here. And then for a detail view in our post, we're using a shortcut called get_object_or_404, which means that if a post with the passed in slug isn't found, Django will return a 404 status code.

[00:05:58] Now, this is a pattern that you will see over and over in Django. And so Django has built in some handy shortcuts called class based views that allow you to take out a lot of this stuff that you might do over and over again. So I'm going to check out the class_based_views branch in the repository.

[00:06:21] And we're gonna take a look at what class based views look like. This is it. So from django.views.generic, I've imported DetailView, which means give me one object, and I've imported ListView, which gets a list of objects. And I've also imported the model that we care about, in this case it's Post.

[00:06:47] So I'm creating a class-based view called PostListView that inherits from ListView. And I'm telling it what model to use. If we take a look at our URLs again, they look a little bit different. Now, you notice that I didn't have the about page in here. That's because I'm using a class based view called TemplateView.

[00:07:10] And I'm using it right in URLs. So I'm calling a method on it called as_view and I'm telling it what template name I care about. Super quick shortcut. I'm also calling as_view on my imported PostListView and PostDetailView and giving it a name. Now this code does the same exact thing that the other code was doing with a lot less craft.

[00:07:36] So let me make sure that my server is still running and take a look here. And here we are, everything still works as expected.