Exception Type:TemplateDoesNotExist (post/post_list.html)

Hi folks,
I’m new to django and following this website (Chapter 4: Message Board App | Django For Beginners)
Structure of my file is like :
main_prj,SQL,app_name,manage.py
app_name → other files… ,(templates → app_name-> HTML file)
I’ve used ListView template in views.py & error is coming

I don’t have any post_list.html file.
Why this error is coming and how to resolve it.

This is most likely an error in your view.

Please post the view.

Note: When posting code here, do not post images of code. Copy/paste the code into your reply, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

Views.py file

from django.views.generic import ListView
    from .models import Post

class HomePage(ListView):
           model = Post
           template_name = "home.html"

templates structure in the app
App_name → templates → App_name → home.html

Why it is creating post_list.html

home.html

<h1> Message board homepage </h1>
<ul>
{% for post in post_list %}
<li>{{post.text}}</li>
{%endfor%}
</ul>

First, the reference to post_list.html is a reference to the default view name for a ViewList. If it can’t find the view named in the template_name attribute of the class, it will try to find that default view. So the root cause here is that it’s not finding your home.html template.

Now, note that the tutorial has:

In your text editor, create a new file called templates/home.html

So the tutorial is written to have you put your template directly in the templates directory, not within a subdirectory for the app.

You can either change the location of your home.html template or change your reference in the template_name attribute to match its actual location.

I have put the path of the home.html in template_name.
Ex:
In views.py file

template_name = “app_name/templates/app_name/home.html”

Still not working.

What is your TEMPLATES setting in your settings file? (Specifically the DIRS setting)

'DIRS' : [BASE_DIR/ "post/templates/post"],  # app_name/templates/app_name

Just to be explicitly clear, you have written:

and:

but your DIRS setting is:

What directory contains your “home.html” file?

Okay,my bad.
Actually above two code app_name I’ve given example.
App_name = post

So,home.html is in "post/templates/post " → (home.html)

Ahh, I think I see it now.

The TEMPLATES DIRS setting is already relative to your project root, it’s not an absolute path. You should not prepend BASE_DIR on to the DIRS entries.
(Or to be more precise, it’s generally defined that way. It can be absolute, but then in this case, you’d want to verify that the entry being created is accurate.)

Side note: Additionally, if you’re going to store and use templates in apps, you may also want to set the APP_DIRS setting to true. In the “common case”, DIRS is set to a project-level templates directory while the APP_DIRS setting is used to specify that templates could exist in the individual app_dir/templates directory.

My APP_DIRS setting is true.
I mean can u please tell me the syntax, what should I write to remove the post_list.html error.

At this point then, my recommendation for you would be to follow the tutorial exactly. Make all settings and file locations the same as what the tutorial defines. Start with something known to be correct, then if you wish to change it, make those changes.

Still same problem is coming it is searching for post_list.html.

I’ve changed the templates from app to project.

My Files structure:
main_prj,post,templates,sqlite,manage.py

templates structure (templates → home.html)

settings.py

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

Error:

django.template.exceptions.TemplateDoesNotExist: post/post_list.html

What to do now ?

Please post your urls.py file.

Also, you’re showing a view named HomePage. Do you have any other views in that file?

post (app) [urls.py]

from django.urls import path
from .views import HomePage

urlpatterns = [
path('',HomePage.as_view(),name = 'home'),
]

post [views.py]

from django.shortcuts import render
from django.views.generic import ListView

from .models import Post

# Create your views here.

class HomePage(ListView):
    model = Post
    template = "home.html"

Note: The attribute is template_name, not template.

1 Like