Home > Back-end >  Why did I get Template does not exist error, though it appears Django offers flexibility with respec
Why did I get Template does not exist error, though it appears Django offers flexibility with respec

Time:01-13

I have read several posts here where people discussed their preferred directory structure to keep various files of their Django project. But I get a file-does-not-exist error if I put my templates in the inner my_project folder instead of the outer my_project folder which contains the manage.py file. Is there some config file that specifies the default location of the templates (and other files)?can you please help me with the command change if I put templates in the inner (sub) folder? I just began my first Django project and would appreciate your help.

CodePudding user response:

Ok, django looks for your templates at default places. I recommend you put your html files there (as a beginner).

Default places are:

- my_site_project/
-- templates/
--- base.html
--- navbar.html
--- footer.html
-- my_app1/
--- templates/
---- my_app1/
----- index_app1.html
----- about_app1.html
-- manage.py

If you put a general templates/ folder inside your project, same folder as your manage.py, those templates will be pulled.

If you want app specific templates the convention is to put a folder inside your app, again called templates/. This can lead to overlap if you put index.html inside your app-templates and an index.html inside your general templates/. Therefore you need to namespace the templates of your app. Create the app; create a folder called templates/ inside it; inside just created templates/folder you create another folder called my_app1; in that folder create your .html files.

Read the tutorial here. It is probably easier to understand than my jibberish. It is the official django tutorial.

A part of it:

First, create a directory called templates in your polls directory. Django will look for templates in there.

Your project’s TEMPLATES setting describes how Django will load and render templates. The default settings file configures a DjangoTemplates backend whose APP_DIRS option is set to True. By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS.

You can, in your settings.py via DIRS, specify where django looks for your templates. Although as a beginner I do not know why you should not stick to the conventions first.

  • Related