Django's Double Folder Structure
In the previous lesson, you created a template, and now you’re going to see how to set it up. In production, Django eventually collects all of the templates in various folders and puts them all together in one folder for all the templates.
That can work fine, but there can be some problems with a flat folder structure, so it can be best to use a double folder structure instead.
In the previous video, we created a template and linked it correctly, so now you might be wondering, “Why do you not just drop this
index.html right into the
templates/ folder? Why is there this extra folder in there?”
00:13 I want to show you why that’s the case. So, remember that this is the better way to set it up: with this nested folder structure. Let’s take a look at why that’s the case. So, we were thinking about maybe we just do a flat folder structure. How would that look like, and is there a problem with it?
Assume that in our example Django project, we have three apps. We have the
projects app, the one we just created, and the
templates/ folder, inside of there. We have another app with a
templates/ folder and yet another app with a
What Django does is that in production, eventually, it collects all of those templates, everything that sits inside of a folder called
templates, something that you define inside of these template directories, and puts it all together into one folder for all the templates.
They all come together. Django is going to know which one to find and which one to render. However, there’s some possible troubles with this flat folder structure, and that’s why we want to avoid it. So, we’ve created this
index.html file, which is a very common name for an HTML file.
Let’s assume that all of our apps might have an
index.html file. What happens in production? All of those files get pulled together into the big
templates/ folder, and Django doesn’t know which
index.html files to render, so there is a problem here. Now, this double folder structure that we created before, is the solution to this issue. If, inside of each of those folders, we create another one that just has the same name as our app—so,
projects/templates/ and inside, other
app2/templates, another folder called
app2, et cetera—and then inside of there, we can have
index.html files in each of them. It doesn’t matter.
02:18 We don’t have to worry about double naming, which—as long as your app is really small—might not be a problem, but if the project grows and you have multiple apps in there, this could become a problem.
You might accidentally have the same named templates and then run into unexpected problems. If you have it set up like this, with a double folder structure, then what Django does is it collects everything, it collects the folder as well as the files, and puts them all together into this big
That’s great because then, this makes the paths unambiguous, and it’s always clear and obvious for Django what is the right template to render. So eventually, you’re going to end up with a folder structure like this: the big
templates/ folder containing everything, containing a folder that’s named after your app, and inside of there, the app-specific templates.
That gets a big green checkmark because we’re avoiding some problems. All right. So, I hope this explains to you why we want to do this double folder structure like this
03:23 It’s essentially just to avoid some troubles that you could run into in the future. Let’s start with good habits right from the beginning. Okay, that’s it for this video. Next, we’re going to change our HTML template a little bit and add Bootstrap styling to it. See you in the next video!
Become a Member to join the conversation.