Django's Double Folder Structure: Nesting
Key Takeaways
Django's Double Folder Structure: Nesting, using Django templates and nested folder structures to avoid naming conflicts in production.
Full Transcript
the previous video we created a template and licked 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 and 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 and let's take a look 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 of Django project we have three apps so we have two projects app the one we just created and the templates folder inside of there we have another app with the templates folder and yet another app with the templates folder 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 defined inside of this template directories and puts it all together into one folder for all the templates so this is not necessarily a problem yet and this can work fine so I have like index.html here I have site dot HTML there and I have project list dot HTML here they all come together jangles gonna know which one to find and which one to render however there are 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 protection 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 did we create it before it's the solution to this issue if inside of each of those folders we create another one that just has the same name as our hep so projects templates and inside and other projects folder yep two templates another folder called f2 etc and then inside of there we can have index dot HTML files in each of them it doesn't matter we don't have to worry about double naming which as long as your app is really small this might not be a problem but if the project grows and you have multiple apps in there this could become a problem and you might just 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 jenga does is it collects everything it collects the folder as well as the files and puts them all together into this big templates folder and 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 to help specific templates and that gets a big green checkmark because we're avoiding some problems so I hope this explains to you why we want to do this double folder structure like this projects templates projects 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 and that's it for this video next we're gonna change our HTML template a little bit and add bootstrap styling to it see you next video
Original Description
In production, Django 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.
Click here to learn more: https://realpython.com/courses/django-portfolio-project/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Real Python · Real Python · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A better Python REPL – bpython vs python interpreter
Real Python
Introducing large-type.com – A Utility Website
Real Python
Reading Hacker News Without Wasting Tons of Time
Real Python
Forward References and Python 3 Type Hints
Real Python
Using Sublime Text as your Git Editor
Real Python
Python Code Linting and Auto-Complete for Sublime Text
Real Python
Make your Python Code More Readable with Custom Exceptions
Real Python
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
How to Use Sublime Text from the Command Line
Real Python
Rename Variables with Multiple Selection in Sublime Text
Real Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
Sublime Text Whitespace Settings for Python Development
Real Python
Function Argument Unpacking in Python
Real Python
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
Using "get()" to Return a Default Value from a Python Dict
Real Python
A Python Shorthand for Swapping Two Variables
Real Python
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
Setting up Sublime Text for Python Developers
Real Python
Sublime Text + Python Guide Overview
Real Python
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
Type-Checking Python Programs With Type Hints and mypy
Real Python
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
My Python Code Looks Ugly and Confusing – Help!
Real Python
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
Programmer Portfolio – Example and Walkthrough
Real Python
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
How to Build Your Public Speaking Skills as a Developer
Real Python
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
Cool New Features in Python 3.6
Real Python
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
Emulating switch/case Statements in Python with Dictionaries
Real Python
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
A Crazy Python Dictionary Expression ?!
Real Python
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
Optional Arguments in Python With *args and **kwargs
Real Python
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
Installing Python Packages with pip and virtualenv / venv
Real Python
"For Each" Loops in Python with enumerate() and range()
Real Python
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
Python Tutorial: List Comprehensions Step-By-Step
Real Python
Leveraging Python's Implicit "return None" Statements
Real Python
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
Writing automated tests for Python command-line apps and scripts
Real Python
How to find great Python packages on PyPI, the Python Package Repository
Real Python
Immutable vs Mutable Objects in Python
Real Python
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
My Experience at PyCon 2017 in Portland
Real Python
Pylint Tutorial – How to Write Clean Python
Real Python
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python
More on: AI Pair Programming
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI