MohitC MohitC - 1 month ago 10
Python Question

Templates render properly on deployment server but TemplateDoesNotExist error on runserver

I have a project structure like this

mainproject
├── manage.py
├── mainproject
│   ├── settings.py
│   ├── templates
│   │   └── mainproject
│   │   └── index.html
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
└── app
├── admin.py
├── apps.py
├── forms.py
├── models.py
├── templates
│   └── app
│   ├── index.html
│   └── abc.html
├── urls.py
└── views.py


Now in my settings.py I have

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['mainproject/templates', 'app/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',
],
},
},
]


When I use this on main domain on my deployment server, (deployed using nginx and uwsgi), it works all well.

I am able to access mainproject's index at
domain.com
and app's index at
domain.com/app


But when using
runserver
, only
domain.com:8000/app
works and
domain.com:8000
gives error
TemplateDoesNotExist at /
.

Why so and how this could be fixed?

Template loader postmortem:

Using engine django:
django.template.loaders.filesystem.Loader: /home/mohit/mainproject/templates/mainproject/index.html (Source does not exist)
django.template.loaders.filesystem.Loader: /home/mohit/app/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/Env/mainproject/lib/python2.7/site-packages/django/contrib/admin/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/Env/mainproject/lib/python2.7/site-packages/django/contrib/auth/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/mainproject/app/templates/mainproject/index.html (Source does not exist)


If I change
DIRS
line in
TEMPLATES
in settings.py and make it

'DIRS': ['mainproject/mainproject/templates', 'app/templates'],


Then it works on runserver and doesnt work on deployment server.

Answer

As per comments by xtranophilist and Amar, I used relative path as follows and it worked:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
....

Then in template dirs:

'DIRS': [BASE_DIR + '/mainproject/templates']