Sunday, March 05, 2017

How to resolve Django 1.10: TemplateDoesNotExist while referring to specific *.html

As per the new standard and frame work provided by Django it is suggested to put all the *.html files inside a template folder of specific application
For example lets assume we had an Django applciation with following folder structure

-siddhuDjangoapplication (Eclipse Folder name of Django Applcation)
-siddhuDjangoapplication (Main Django Applcation where all our files is placed)
- __inti__.py
- setings.py
- urls.py
- views.py
- wsgi.py
-manage.py

Now if we are creating our UI using *.html i.e. siddhu.html and place directly inside the siddhuDjangoapplication (Main Django Applcation where all our files is placed) and if we try to access the ui files we will get this error on the screen
-siddhuDjangoapplication (Eclipse Folder name of Django Applcation)
-siddhuDjangoapplication (Main Django Applcation where all our files is placed)
- __inti__.py
- setings.py
- urls.py
- views.py
- wsgi.py
- siddhu.html
-manage.py

TemplateDoesNotExist at /siddhuDjangoapplication/
/siddhu.html
to Over come this please make following changes give below
step 1:- Open your settings.py and under TEMPLATES dictionary add following line
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Step 2:- Create a folder called as templates inside siddhuDjangoapplication (Main Django Applcation where all our files is placed).
As we had different module in that case it is best practise to make ui component in individual folder i.e. template/siddhuDjangoapplication

So our new folder structure will be
-siddhuDjangoapplication (Eclipse Folder name of Django Applcation)
-siddhuDjangoapplication (Main Django Applcation where all our files is placed)
- __inti__.py
- setings.py
- urls.py
- views.py
- wsgi.py
- templates
       - siddhuDjangoapplication
           - siddhu.html
           - siddhu2.html
-manage.py

No comments: