'Django: base.html "TemplateDoesNotExist at / error" and project structure
When in my home.html (or any other .html) I try to extend my base.html file with this
{% extends "static/src/base.html" %}
I get this error:
TemplateDoesNotExist at / error
Also, I'm about to start my first "serious" project and so I'm trying to do things a little less amateurish starting with my project structure. This is what I've come so far following the clues I got from other site, but I'd like to have some opinion from people with more experience than me if this is a good way to start the project. In particular, I'm a little confused about where to put the base.html file. Is it ok to put it in the src folder or should I put it with the navbar.html/footer.html/etc page or navbar.html/footer.html/etc should go in the src folder instead?
This is my project structure:
website_name
- core (where is the settings.py and where I put homepage, news page, contact and about us page...)
- app name_1 (like members)
- app name_2
- media
-- img
-- upload (user ulploaded excel file)
--- excel_file.csv
-- download (for user to download file)
- static
-- src
--- base.html
--- style.css
-- dist
--- style.css
-- node_modules
-- [...]
- template
-- core
--- home.html
--- news_list.html
--- news_delete.html
--- [...]
-- app name_1
-- app name_2
-- navbar.html
-- footbar.html
-- [...]
Thank you for your help!
Solution 1:[1]
normal structure is to put templates in app folder:
- core
-- templates
--- core
---- my_base_template.html
- app_name1
-- templates
--- app_name1
---- my_base_template.html
- app_name2
-- templates
--- app_name2
---- my_template1.html
{% extends "app_name1/my_base_template.html" %}
....
---- my_template2.html
{% extends "core/my_base_template.html" %}
With settings correctly setup the template engine will look for template files in the app/templates/ folders automatically.
So please check the settings.py for TEMPLATES -> APP_DIRS=True from Django docs https://docs.djangoproject.com/en/4.0/topics/templates/ :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},
]
logically templates are not static but are rendered at run time, so they do not make sense inside the static folder structure.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
