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.

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.

00:00 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?

00:34 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 templates/ folder.

00:48 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.

01:04 So, this is not necessarily a problem yet,

01:08 and this can work fine. I have, like, index.html here, I have site.html there, and I have project_list.html here.

01:17 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.

01:34 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 projects/ folder. 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.

02:29 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 templates/ folder.

02:48 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.

03:09 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 projects/templates/projects/.

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!

reblark on Oct. 31, 2019

Is this “universal” templates folder hidden?

reblark on Oct. 31, 2019

So that I understand the file structure more clearly, the first thing I do is create a folder to put all my work for django in, for instance django-portfolio. But then I create a sub-folder, for instance projects, which becomes my apps central folder(it could be “portolio”. In that folder, I put whatever apps I want to build in this structure. So, I might build an app called “projects” and an app called “monies” and an app called “travels.” Inside each of these apps will be the files from to and inside the app will be the templates that will be used by each of the apps above and contained in that app structure. So, all the files in in each app will have the app name in its path and live in the templates folder as app/commonname. The point being that in the total structure, templates lives above all the app structures and collects all the templates from all the apps into its one folder. (Not sure about how well I wrote this but I think I have the point.)

deodesumitsingh on Dec. 28, 2019

I learned alot with this single video. Thank you alot for sharing Django project structure in simple terms.

Martin Breuss RP Team on Jan. 23, 2020

Glad it was useful @deodesumitsingh! :)

Samuel Barbosa on Jan. 28, 2020

I had never paid attention to that detail. Thanks @mbreuss

Ken on May 15, 2020

Really liking this tutorial so far. Most clear and understandable one I have run into so far. Haven’t had any questions pop up that weren’t answered. Other tutorials just were confusing.

Hector Perea Mosquera on July 13, 2020

So far, the best Django tutorial that I’ve seen so far. Very useful, and the folder structure explanation was very clear. Thanks a lot for this.

CCC on Oct. 9, 2020

Thank you for visualizing the problem, this explained the problem and the solution in very clear terms!

Lots of tutorial online would gloss over this or not even mention it until it’s a problem, but you show us best practices from the beginning even if it may confuse some.

Many thanks Martin, you’re a great teacher, mate.

Martin Breuss RP Team on Oct. 9, 2020

Thanks for all the nice comments @Samuel Barbosa, @ken4, @Hector Perea Mosquera and @CCC! 😀 Glad you’re finding the explanations helpful.

I remember being pretty confused about errors coming from an incorrectly set up folder structure myself and how a big lightbulb turned on when I finally got it 💡 !

abykal on April 3, 2021

Hi, I have a question. Instead of making templates folder in each app, why can’t me make a single template folder in main project directory, with separate directories for each app? This way, we can even have common templates under main directory.

Any comments?

abykal on April 3, 2021

Also, it was mentioned in slide that structure of ‘templates’ was within main project. But, PyCharm has ‘templates’ created under projects app. PyCharm - screenshot

Slide - screenshot

Bartosz Zaczyński RP Team on April 6, 2021

@abykal It’s a matter of preference. However, one advantage to keeping the templates in the app subfolder is portability. That way, you can reuse the same app, including its templates, in a different Django project.

Become a Member to join the conversation.