我对Django 非常陌生.目前正在阅读《Django for初学者》(https://djangoforbeginners.com/),在第4章之前一切都很好.我得到了以下错误:

TemplateDoesNotExist at /

home.html, posts/post_list.html

我不知道这是从哪里来的.为什么Django要查找post/post_list.html文件?我指的不是AFAIK任何地方的那个文件.

我收到以下错误消息:

Request Method:     GET
Request URL:    serverurl
Django Version:     2.1
Exception Type:     TemplateDoesNotExist
Exception Value:    

home.html, posts/post_list.html

Exception Location:     /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/template/loader.py in select_template, line 47
Python Executable:  /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/bin/python3
Python Version:     3.10.12
Python Path:    

['/home/tang/LinuxServ/message_board_django',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages']

Server time:    Fri, 13 Oct 2023 10:31:52 +0000

Django tried loading these templates, in this order:

Using engine django:

    django.template.loaders.filesystem.Loader: /home/tang/LinuxServ/message_board_django/templates/home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/admin/templates/home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/auth/templates/home.html (Source does not exist)

Using engine django:

    django.template.loaders.filesystem.Loader: /home/tang/LinuxServ/message_board_django/templates/posts/post_list.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/admin/templates/posts/post_list.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/tang/.local/share/virtualenvs/message_board_django-jJcCRH2C/lib/python3.10/site-packages/django/contrib/auth/templates/posts/post_list.html (Source does not exist)

Does it come from my file structure? screenshot showing project structure

#posts/view.py

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




class HomePageView(ListView):
    model = Post
    template_name = 'home.html'
<!-- templates/home.html -->

<h1>Message board homepage</h1>

<ul>
    {% for post in object_list %}
        <li>{{ post }}</li>
    {% endfor %}
</ul>

设置文件:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))




DEBUG = True

ALLOWED_HOSTS = ['*']



INSTALLED_APPS = [
    'posts.apps.PostsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mb_project.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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mb_project.wsgi.application'



DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


STATIC_URL = '/static/'
/posts/urls.py
from django.urls import path

from .views import HomePageView


urlpatterns = [

    path('', HomePageView.as_view(), name='home')

]
#urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('posts.urls')),
]

推荐答案

你的template directory configuration is incorrect号.如果只配置template,Django将搜索./templates中的文件,但文件在./mb_project/templates

应该是这样的:

'TEMPLATES': [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'mb_project/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',
            ],
        },
    },
],

Python-3.x相关问答推荐

"安装serial vs安装psyserial header,"""

Strawberry FastAPI:如何调用正确的函数?

对大型数据框中的选定列进行重新排序

两个 y 轴在零处对齐的 plotly barplot

从列表的元素和python中的多个多索引数据帧执行方程

我正在使用 python 线程,当查询 mysql 时,代码似乎在运行并保持在无限循环中,没有返回任何错误

如何使用 Selenium 和 Python 作为线程来使用事件(Chrome-Developer-Tools)?

python2和python3中的列表生成器

XPATH:使用 .find_elements_by_xpath 为未知数量的 xpath 输入值

使用 from re findall 组合连续匹配并分离非连续匹配

是否将dict转换为一个数据帧,每个值都有重复的键?

python中两个连续的yield语句如何工作?

为什么 List 不能包含多种类型?

如何并行化文件下载?

为什么不切换到 Python 3.x?

str.format_map(mapping) 和 str.format 有什么区别

如何在 Spyder 控制台中使用变量执行 Python 3.3 脚本?

如何使用请求发送带有标头的 PATCH 请求

在 Python 3 中调用 super() 的 4 种方法中的哪一种?

无法解码 Python Web 请求