添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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
Screenshot from 2022-09-26 02-45-32 1325×524 92.5 KB

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

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>
{% for post in post_list %}
<li>{{post.text}}</li>
{%endfor%}
              

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.

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.

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 ?

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"