While going through the development of a Django web app we go through the various commands such as django-admin startproject myproject, django-admin startapp my app, etc. After performing the basic command a folder with the name will be created in your workspace inside that you will find a manage.py file.
To check whether the Django app is been created or not pass the command in your terminal as python manage.py runserver.
After the command gets executed you will find that the server is running in your localhost at post 8000. You faced the TemplateDoesNotExit error that means you have done all the above commands.
As we have created a templates folder inside the myapp folder. now the structure inside myproject directory is smiler to:
├── myproject
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── myapp
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── templates
│ │ └── your_app_name
│ │ └── my_index.html
│ ├── urls.py
│ └── views.py
├── manage.py
├── DB.sqlite3
Internal server error: TemplateDoesNotExit
TemplateDoesNotExit error occurs when the server call for the URL given by the developer to be redirected to a certain page and the render function doesn't get the template provided by the developer.
To solve this error first, go to your setting.py file which is inside of the /myproject/myproject/
folder, and add a few lines inside the setting.py file.
Steps to follow:
1. Add your app to INSTALLED_APPS.
INSTALLED_APPS = [
...
'myapp',
...
]
2. Find this tuple inside the setiing.py.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
3. Create a variable name TEMP_DIR
you will find a variable named as BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Now, create a variable with name TEMP_NAME = os.path.join(BASE_DIR,'myapp/templates')
On the above TEMP_DIR change the 'myapp/templates' to your template name if it is in your app folder change 'myapp' as your app-name '/' name of the template. If the file structure is in the below form then just in place of 'myapp/templates' put your template folder name as 'templates'.
├── myproject
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── myapp
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── urls.py
│ └── views.py
├── templates
│ │ └── your_app_name
│ │ └── my_index.html
├── manage.py
├── db.sqlite3
4. Add the TEMP_DIR in the TEMPLATES tuple.
At first, your TEMPLATES tuple was as bellow:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
In the 'DIRs':[]
, add your TEMP_DIR
.
After the change the tuple will be as follow:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMP_DIR],
'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',
],
},
},
]
5. Now run the python manage.py runserve
r to check whether the problem is resolved or not.
As you have specified the right template folder path. you will not find this error again.