Lessons: 19Length: 1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.5 Saving Time With the Render Function

You have probably noticed that our code for these views has been fairly repetitive so far. The problem with that is that it opens up the doors for more mistakes. Luckily, Django offers a solution to this problem: the render function.

4.5 Saving Time With the Render Function

So as I mentioned in the previous lesson, we're starting to get a little repetitive here where we're getting some data. We're getting a template, we're creating a context. And then we're using that template to render that context along with the request. Well, like I said, Django does provide some shortcuts. So we want to start taking advantage of some of those things now so that our application doesn't balloon up with a lot of extra boilerplate code that we don't really need. So the first thing that we want to do is we're going to come in here and we're actually going to get rid of this line right here with the loader. So that will work and that's the basic way to do things. But we can do them a little bit more efficiently. So now I want to say from django.shortcuts, I want to import a function called render. Now, basically what render is going to do under the covers is it's gonna handle all of those steps that I just talked about. So now we have our applications, and that's good, we still want that. But now I can skip this line of getting the template. I don't really need that anymore. I am gonna create my context. But now down here, I can get rid of all of this right here. And I can simply say that I want to render. And what do I want to render? Well, I want to render. I want to pass in my request. Just like I did before. I wanna pass in my template. So I'm gonna say fertilizer/index.html. And then I'm also going to pass in my context. And this is actually going to return an HTTP response. So now I don't have to worry about doing all of those separate steps, I've been able to condense them down in a little bit, which makes my function a little bit shorter, a little easier to maintain. So let's do the same thing here as well. Let's go head and copy this name or cut the name of my template, so I can get rid of that. And then I can come down here and I'm gonna replace all of this to just say render, and I want to render my request. I want to pass in my name of my template. And I'm gonna send in the context as well. So let's go ahead and save that. Now, if I've done everything correctly, I should be able to come back over to my application, rerun it, and you're gonna see that everything works fine for this page. And then we can also go back to fertilizers. And you're gonna see I still get everything here. Now if I click on Milorganite or if I click on Ringer, of course those things don't exist yet because I have to create those templates and those actual pages in the context and bind them together. But at least now we can shorten things up and use just a simple render function to kind of condense some of that duplicate boilerplate code to make things a little easier on ourselves.

Back to the top