django & the “TemplateDoesNotExist” error

非 Y 不嫁゛ 提交于 2019-12-02 22:50:56

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',
]

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/',
)

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

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.

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 to in any extends statements or where you use static to link to files.

just set DEBUG to false in your Django settings file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!