9đź‘Ť
Holy mother of god! I solved it!
I do not know why – but this is the solution to the “TemplateDoesNotExist” error (in my case).
My folder structure is like this:
netz2 > skateproject
till now i had the templates folder in skateproject and in settings.py i pointed to this directory.
this threw the template does not exist error when I tried to open the page in firefox.
as skateproject is the project folder in there ive got an folder sk8 – which is the app that im currently working on and that im trying to execute.
The solution is super simple.
I had to move the templates in the subdirectory of the app. which looks like this
netz2 > skateproject > sk8 > templates
and now it works!
So if you have the same problem, make sure your templates folder is not in the root of the project but is a subdirectory of the app youre working on – AND add this path to the settings.py Template_dirs
it looks like this in my example:
TEMPLATE_DIRS = (
r'H:/netz2/skateprojekt/sk8/templates/',
)
15đź‘Ť
One of the solution to this problem is you should add apps to in settings.py. I assume you have an application named such as invoice then solution to this is
INSTALLED_APPS = [
'invoice.apps.InoviceConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
- Django migration relation does not exist
- Resize image on save
- Django querysets + memcached: best practices
- Django REST Framework: Validate before a delete
9đź‘Ť
If anyone is trying this with Django 1.7 and Python 3 or higher, I did this:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'mysite_dir/templates')]
where myfile_dir can be any dir under mysite. File structure would be something like this:
manage.py
mysite/
mysite/
mysite_dir/
templates/
____init__.py
models.py
#...
another_app/
This answer may seem repetitive but I know I always like to see things from a more recent date… so hope this helps
- Python venv not creating virtual environment
- 'admin' is not a registered namespace in Django 1.4
- Django template tag: How to send next_page in {url auth_logout}?
- Django admin enable sorting for calculated fields
3đź‘Ť
I struggled with a similar issue for a good several hours and found that I could get the template to load if I put it in a /templates subdirectory within the app using the template. You don’t even need to set TEMPLATE_DIRS in settings.py, the file is found automatically. Unfortunately the TemplateDoesNotExist exception is not very descriptive and in some cases literally wrong. If you see in the Template-loader postmortem something similar to the following:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/me/mysite/myapp/index.html
And the file path looks correct for the template you’re trying to load, it’s likely that the file exists but it’s not accessible. Try moving it to a /templates subdirectory of the app.
- How do I write a single-file Django application?
- Clean Up HTML in Python
- Why can't I upload jpg files to my Django app via admin/?
3đź‘Ť
If you follow Django’s documentation, they recommend namespacing your templates, where under templates, you have /yourapp/ again before the templates. See here.
So your structure might look like:
Project Folder
--Project
--yourapp
--templates
--yourapp
--layout.html
But what they don’t make clear is that any hardcoded references to those templates (for example, the render function), need to also prepend your app name to those as well! I thought Django was doing a recursive search for us, but I gave it too much credit. You need to point to the exact link.
e.g.
return render(request, "login.html")
goes to
return render(request, "orders/login.html")
You’ll likely need this too in any extends statements or where you use static to link to files.
0đź‘Ť
For me {% include 'some.html' %}
was throwing this error.
Make sure to include the leading ./
like:
{% include './some.html' %}
0đź‘Ť
Just paste the path of your templates directory from your source root in your project settings dirs variable. It should look something like this
'DIRS': [
'templates'
],
- Django how to reconnect after DatabaseError: query timeout
- Django rest framework api_view vs normal view
- How to properly runserver on different settings for Django?
- Django request.user.is_authenticated is always true?
- Django load local json file
- Django days-of-week representation in model
- Why isn't my Django User Model's Password Hashed?
- In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)?