0

So , this is my first ever try with django and im currently learning about urls and views, ive been encountering the error that says "template not found " i apologize beforehand if the question is noob-ish. (here is the code for urls.py file)

`"""
URL configuration for views_urls project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home)
]
`

(below is the code for the view.py file that i created , which exists in the same folder as the url.py file)

`from django.shortcuts import render

def home(request):
    return render(request, 'main.html')`

(below is the main.html file the exists in a folder called templates)

`<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>main</title>
    
</head>
<body>

    <h1>The main html page is working </h1>
    
</body>
</html>`

(below are the changes i made in the settings.py)

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

ps. ive made sure the spellings are all correct. i am still recieveing an error that says TemplateDoesNotExist at / main.html

kindly help, thank you.

What i was expecting was the contents of my main.html file to be displayed , but i got an error that saying template does not exist. Ive tried rechecking if django was successfully installed and it was.

Help would be greatly appreciated.

  • In debug mode the error page where it was attempted to load the template from. Make sure it is found at one of these locations. – Klaus D. Apr 11 '23 at 08:57
  • 1
    You could try one of the many solutions given here - https://stackoverflow.com/questions/1926049/django-templatedoesnotexist – Sarthak Khandelwal Apr 11 '23 at 09:01

3 Answers3

1

You need to have your main.html file inside the folder /your_project/your_app/templates/main.html.

NixonSparrow
  • 6,130
  • 1
  • 6
  • 18
0

Did you add your app in settings?

INSTALLED_APPS = [
    "YourAppName",
    # ...
]

If you don't do it, your Django project will not be able to "see" the files inside your app.

Cezary
  • 33
  • 3
0

In your settings templates config, instead of just adding 'templates', add the given below code

'DIRS': [ BASE_DIR / 'templates'],