Django 中的模板继承错误与 TemplateDoesNotExist

时间:2021-03-23 18:45:54

标签: python django django-templates

enter image description herebase.html 和子 html 文件在一个目录 app/templates/app 中: Lead_list.html

{% extends "leads/base.html" %}

{% block content %}
    <a href="{% url 'leads:lead-create' %}">Create a new Lead</a>

    <hr />
    <h1>This is all of our leads</h1>
    {% for lead in leads %}
        <div class="lead">
            <a href="{% url 'leads:lead-detail' lead.pk %}">{{ lead.first_name }} {{ lead.last_name }}</a>. Age: {{ lead.age }}
        </div>
    {% endfor %}
{% endblock %}

base.html

    {% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DJCRM</title>
    <style>
        .lead {
            padding-top: 10px;
            padding-bottom: 10px;
            padding-left: 6px;
            padding-right: 6px;
            margin-top: 10px;
            background-color: #f6f6f6;
            width: 100%;
        }
    </style>
</head>
<body>
    {% block content %}
    {% endblock %}

</body>
</html> 

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Lead, Agent
from .forms import LeadForm, LeadModelForm

def lead_list(request):
    leads = Lead.objects.all()
    context = {
        "leads": leads
    }
    return render(request, "lead_list.html", context)

settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

我收到 django.template.exceptions.TemplateDoesNotExist 的错误:lead_list.html 我想我已经做对了一切,并与文档进行了比较,但找不到我的错误。 提前致谢!

1 个答案:

答案 0 :(得分:1)

您使用以下命令渲染模板:

def lead_list(request):
    context = {
        'leads': Lead.objects.all()
    }
    return render(request, 'leads/lead_list.html', context)

APP_DIRS 设置意味着 Django 将查看应用程序的 templates/ 目录,但由于此类目录不直接包含 lead_list.html 文件,因此会引发错误。然而,模板目录中的 leads 目录中有这样的模板。

相关问题