Django 3无效模板工程师错误

2024-10-01 00:25:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图加载详细页面html模板,但似乎抛出了一个错误。我正在尝试调试django的新功能,但大部分时间我都能找到答案。但在这件事上完全不知所措

与setting.py或template视图或url中的模板引擎有关。我认为这是模板设置中的一些内容

我做了一些搜索,但运气不好。virtual studio中的调试控制台中没有错误。就在我打开html模板的url时。任何关于如何更好地阅读错误的反馈或提示都将不胜感激


"Set a unique NAME for each engine in settings.TEMPLATES."

             .format(", ".join(duplicates)))

     return templates

 def __getitem__(self, alias):

     try:

         return self._engines[alias]

…

     except KeyError:

         try:

             params = self.templates[alias]

         except KeyError:

             raise InvalidTemplateEngineError(

                 "Could not find config for '{}'

picture of error

我假设我应该在settings.py的template部分配置basic_app/school_detail.html。这就是我被困的地方

提出无效模板工程师错误( django.template.utils.InvalidTemplateEngineError:在settings.TEMPLATES中找不到“basic_app/school_detail.html”的配置

这就是引发的错误

在/basic\u app/1/

在settings.TEMPLATES中找不到“basic\u app/school\u detail.html”的配置

请求方法:获取 请求URL:http://127.0.0.1:8000/basic_app/1/ Django版本:3.0.4 异常类型:InvalidTemplateEngineError 异常值:

在settings.TEMPLATES中找不到“basic\u app/school\u detail.html”的配置

异常位置:C:\Users\taylo\AppData\Local\Programs\Python\Python38-32\lib\site packages\django\template\utils.py,位于getitem第71行 Python可执行文件:C:\Users\taylo\AppData\Local\Programs\Python\Python38-32\Python.exe Python版本:3.8.2 Python路径:

['C:\Users\taylo\django\Advanced\u django\u CBV\advcbv', 'C:\Python\Python37\Lib', 'C:\Python\Python37\DLLs', 'C:\Python\Python37\Lib\Lib-tk', 'C:\Python\Python37\Scripts', 'C:\Python\Python37\tcl', 'C:\Python\Python37\Tools', 'C:\Users\taylo\AppData\Local\Programs\Python\Python38-32\Python38.zip', 'C:\Users\taylo\AppData\Local\Programs\Python\Python38-32\DLLs', 'C:\Users\taylo\AppData\Local\Programs\Python\Python38-32\lib', 'C:\Users\taylo\AppData\Local\Programs\Python\Python38-32', 'C:\Users\taylo\AppData\Roaming\Python\Python38\site packages', 'C:\Users\taylo\AppData\Local\Programs\Python\Python38-32\lib\site packages']

服务器时间:2020年5月4日星期一16:24:46+0000

======================================================================================================

设置.py

======================================================================================================


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'


======================================================================================================

School_detail.html

======================================================================================================

{% extends "basic_app/basic_app_base.html" %}

{% block body_block %}

<div class="jumbotron">


<h1>Welcome to the School Detail Page </h1>
    <h2></h2>
    <p><b>Name: </b>{{school_detail.name}}</p>
    <p><b>Principal: </b> {{school_detail.principal}}</p>
    <p><b>Location: </b>{{school_detail.location}}</p>
    <h3><b>Students: </b></h3>

{% for student in school_detail.students.all %}
 <p>{{student.name}} who's student id is {{student.info}}</p>
{% endfor %}

</div>


{% endblock %}

enter image description here

======================================================================================================

basic_app.html

from django.db import models

# Create your models here.
class School(models.Model):
    name = models.CharField(max_length=256)
    Principal = models.CharField(max_length=256)
    location = models.CharField(max_length=256)

    def __str__(self):
        return self.name

class Student(models.Model):
    name = models.CharField(max_length=256)
    info = models.CharField(max_length=256)
    School = models.ForeignKey(School,  on_delete=models.CASCADE, related_name='students')

    def __str__(self):
        return self.name

目录图像

url.py

from django.conf.urls import url
from basic_app import views

app_name = 'basic_app'

urlpatterns = [
    url(r'^$',views.SchoolListView.as_view(),name='list'),
    url(r'^(?P<pk>[-\w]+)/$', views.SchoolDetailView.as_view(), name='detail')

]

views.py

from django.shortcuts import render
from django.views.generic import View, TemplateView, ListView, DetailView
from . import models


class IndexView(TemplateView):
    template_name = 'index.html'

class SchoolListView(ListView):
    context_object_name = 'schools'
    model = models.School
    # returns school_list

class SchoolDetailView(DetailView):
    context_object_name = 'school_detail'
    model = models.School
    template_engine = 'basic_app/school_detail.html'


我试图更改settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        'APP_DIRS': True,

我也改变了这一点,但也不起作用

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')

Tags: pathdjangonameappbasicosmodelshtml