As shown in the title of this article, the main discussion will be focus on how to solve the error message caused which is specifically shown as “TemplateDoesNotExist at /”. The main error itself can be shown in the following image :
The error itself is actually caused in the process of accessing an URL of “http://localhost:8080”. The URL itself is an URL of a development server run with the help of Django utility. But the main problem is where does the actual file executed which is represented by the URL itself. It can be viewed by looking at the following configuration located in a file named ‘urls.py’. The content of the file which is becoming the main source of the problem is shown as follows :
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.home, name='home'), ]
It is represented with the line as follows :
url(r'^$', views.home, name='home'),
It means that the default URL will be redirected to a function or method named home in the file named ‘views.py’. It is actually going to be used from a certain application or module. So, the most important step before defining the URL for accessing the default URL, it is actually importing views from the associated application or module. The URL name is ‘home’. In this context, it is from myapp for an example. So, if the main goal is for importing the view from ‘myapp’, below is the line of code which is representing on doing it :
from myapp import views
The next step is for defining the actual method which is declared in the file named views.py located in a folder which is actually made for an application or module. In this context, the app named ‘myapp’ and it is also representing the folder for the application or module. So, below is the actual tree directory :
user@hostname:~/myproject/myapp$ tree -L 2 . . ├── admin.py ├── apps.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py └── views.py 1 directory, 7 files user@hostname:~/myproject/myapp$
Below is the actual content of views.py in the folder representing application or module named ‘myapps’ for defining method or function named ‘home’ as follows :
def home(request): context = locals() template = 'home.html' return render(request,template,context)
So, the next step is for creating a file named ‘home.html’ in order for the URL accessed can be processed and it will be redirected to the specififed page named ‘home.html’. It also must be located in a folder named ‘templates’. Create the folder named templates first. Obviously it must be created inside the folder named ‘myapp’. After successfully created the folder, just create a file named home.html. It will solve the problem.
As an additional information, the templates folder will be searched based on the sequence of application defined in the settings.py file located in the main project folder. For an example as follows :
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.sites', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'allauth', 'allauth.account', 'allauth.socialaccount', 'contact', 'task', 'crispy_forms', 'rolepermissions', ]
One thought on “How to Solve Django Error Message : TemplateDoesNotExist at /”