In this lesson, you’re going to create your views and have a visit with your good friend, the
AttributeError! According to this error message, the module
project.views has no attribute called
all_projects, so head over to
views.py to create this view:
# Create your views here. def all_projects(request): return render(request, 'all_projects.html')
Now, you’re getting a new error message,
TemplateDoesNotExist, because you haven’t yet created this template, so Django doesn’t know what to render.
views.py and create this view. We’re looking for one called
all_projects, that’s the one we want to create. If I say
def all_projects(), taking the
request, as always, as input, and we want to return
this slide is already telling us what to expect. If I reload this here, I’m going to get the
TemplateDoesNotExist error. Again, just simply because I have not created this template yet, and that’s why it’s telling me it cannot find this template and doesn’t know what to render. Great!
So, back to here. We know already how to fix this
TemplateDoesNotExist, right? We’re going to have to go in here,
templates/projects/, and create the template in here with the same name. We’re going to do that in just a moment. For now, let’s focus on the view. In this case, we went to make a query to the database.
We want to return all the projects, and the views is where the logic of the code happens. So, what we want to do in here is query the database to return all
Project objects that we have in there. So, you might be wondering how to do that, and it’s actually exactly the same code that we used when we were inside of the Django shell.
So, if you remember what I ran in there to retrieve all the objects from the database, it was something that went like
Project.objects.all(), right? And we want to save this to a variable, so I’m going to say
projects = this.
So, one interesting thing for me when I was learning Django is to understand when is this code going to get called? I think that’s very important, and that’s why we’re putting this
print() statement in here. My server is running, you see that? It’s all fine, but nothing is printed.
It’s the same thing that we got in the Django shell with the exact same code, right? So, all that’s happening is Django is routing through our setup, it’s routing the
request to this view, and then executes the code—whatever code is sitting in here. In this case, we’re making a call to the database and retrieving all objects, printing out those objects to the console—that’s happening down here—and then we’re trying to render a template that currently doesn’t exist yet.
Cool! So, what we want to do, additionally to building out this template in the next video, is we want to be able to access these projects, because currently, I’m not passing them forward, right? All I’m passing forward is the
request object and the name of the template.
Just to make it easy, I’m naming them the same. And I’m going to get rid of this
print() statement, because we don’t need it. So what I’m doing here now, inside of this view, is it’s going to get called when the user accesses this page.
Yes? Then, all the code in here runs, every time that the user accesses this page. I’m going to make a call to our database and retrieve all the
Project objects that are stored in there. And finally, I’m going to return a
render() call that takes the
request, takes the name of the template that I want rendered, and passes these objects that we retrieved from the database forward to the template as well.
06:01 Because then, in the template, inside of a Django template, we’re going to be able to render these objects. So, let’s go ahead and do that in the next video where we’ll create our template and make sure that we’re actually displaying what we got from the database.
Become a Member to join the conversation.