TemplateDoesNotExist at /myapp/ home.html

Hello, I hope you can help me with this problem of trying to load the template
which, even after following different tutorials and steps, always gives the same error.

Here’s my project structure

DjangoProject (Python Folder)
    djangoproject
       djangoproject(settings.py, urls.py, etc...)
       myapp(migrations folder, urls.py, views.py, etc...)
       templates(home.html)

djangoproject/settings.py

ROOT_URLCONF = 'djangoproject.urls'

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',
            ],
        },
    },
]

djangoproject/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("myapp/", include("myapp.urls")),
    path("admin/", admin.site.urls),
]

myapp/view.py

from django.shortcuts import render
from django.http import HttpResponse, request
# Create your views here.


def mainpage_view(*args, **kwargs):
    # return HttpResponse("<h1>Hello World Telmo</h1>")
    return render(request, "home.html", {})


def contact_view(*args, **kwargs):
    return HttpResponse("<h1>Contact Page</h1>")


def about_view(*args, **kwargs):
    return HttpResponse("<h1>About Page</h1>")


def social_view(*args, **kwargs):
    return HttpResponse("<h1>Social Page</h1>")

myapp/urls.py

from django.urls import path

from . import views


urlpatterns = [
    path('', views.mainpage_view, name="home"),
    path('contact/', views.contact_view),
    path('about/', views.about_view),
    path('social/', views.social_view),

]

The error message when I run the server:
TemplateDoesNotExist at /myapp/home.html

Request Method: GET
Request URL: http://127.0.0.1:8000/myapp/
Django Version: 4.2.7
Exception Type: TemplateDoesNotExist
Exception Value: home.html
Exception Location: C:\Users\telmo\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\loader.py, line 19, in get_template
Raised during: myapp.views.mainpage_view
  • django.template.loaders.filesystem.Loader: C:\Users\telmo\OneDrive\Ambiente de Trabalho\DjangoProject\templates\home.html (Source does not exist)

Terminal error shows me this:
Not Found: /myapp/4f64eaa44708eacdfb67703150ce5f05.jpg

Please do not post images of code here. Copy/paste the code into the body of your post, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Also, when asking for assistance with an error, copy/paste the complete error with the full traceback from your console window, into your post, also between lines of ```.

I suggest you delete all the code image posts and create your post appropriately.

Alright thank you , I will change it.

I hope my post now complies with the principles.

Close - you want to use the backtick - ` and not the apostrophe - ' when fencing your code. (I’ve taken the liberty of correcting it for you)

Notice that the error message is telling you where it’s looking for the template;

This means that it’s looking for your templates folder in DjangoProject, and not DjangoProject\djangoproject

You have:

I believe you want this to be:
'DIRS': [os.path.join(BASE_DIR, "templates/")],

AttributeError at /myapp/

module ‘django.http.request’ has no attribute ‘META’

Request Method: GET
Request URL: http://127.0.0.1:8000/myapp/
Django Version: 4.2.7
Exception Type: AttributeError
Exception Value: module ‘django.http.request’ has no attribute ‘META’
Exception Location: C:\Users\telmo\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\context_processors.py, line 41, in debug
Raised during: myapp.views.mainpage_view

The first parameter in every view should be request. Also, you should not be importing request from django.http.

it should be like this ?

def mainpage_view(request):
    return render(request, "home.html", {})

also, where should I import the request from ?

Yes, that view is correct. Review the first tutorial page - Writing your first Django app, part 1 | Django documentation | Django

You don’t. You don’t need to.

The request object is passed as a parameter to your function.

If you have not worked your way through the Official Django Tutorial, I suggest you do so. There are a lot of important concepts presented in it.

First of all I want to say a big “Thank You” for your availability, even though I know I can’t offer anything in return, thank you very much again.
I’ve already done the tutorial on the official django page, I found it very useful but a little confusing, because I find different ways of doing the same thing in different places, but I will continue to study with the hope of one day being a good programmer like you.

Telmo my friend, additionally to Ken’s great responses,

As i see you have home.html file inside templates directly, you need to insert directories in templates directory for each app and then insert inside the html pages.
and in views too the need to be as shown below,

Home View

def mainpage_view(request):
    # return HttpResponse("<h1>Hello World Telmo</h1>")
    return render(request, "myapp/home.html", {})

Structure Tree below for sample.

django_project/
├── templates/
│   └── myapp/
│       ├── home.html

Check this edits i think it will solve the structure in your project.

This is not quite an accurate statement.

There is no requirement that templates be inside an “app” directory within “templates”. It’s a common and recommended practice, and can possibly avoid future problems, but it’s not something that needs to be done.

2 Likes

Thank you for the answer, it makes sense and I will take this into account for the future,
but in this project I will only have this application so I will leave it like that.

1 Like

Thanks Bro for Correcting Me. i thought it was a must.