【问题标题】:Django {% include %} TemplateDoesNotExist at / blog/includes/post.htmlDjango {% include %} TemplateDoesNotExist at /blog/includes/post.html
【发布时间】:2022-02-03 02:24:25
【问题描述】:

你好,祝你有美好的一天,

我目前正在学习 Django 课程,我正在建立一个博客。

我不断收到:

TemplateDoesNotExist 在 / 博客/includes/post.html

我似乎无法解决。

但我认为它与 {%include%} 模板标签有关

我有两个文件: 1)post.html 和 2)index.html

我正在尝试 {%include%} index.html 中的 post.html

但是当我这样做时:

TemplateDoesNotExist 在 / blog/includes/post.html 模板不存在错误

但我已经多次测试了路径 我检查了 settings.py 并且我的路径是正确的

{% include "blog/includes/post.html" %} ### HERE IS THE PROBLEM in index.html
{% include "blog/post.html" %}
{% include "blog/templates/blog/includes/post.html" %}

我已经尝试了所有路径都不起作用。

我可以使它工作的唯一方法是不使用 {% include%} 而是直接复制和粘贴 post.html 但这是不切实际的,不会让我继续我的项目

这是目录结构:

blog/templates/blog/includes/post.html

blog /templates / blog /index.html

##### index.html   ######

{% extends "templates/base.html" %} 
{% load static %}

{% block title %} 
My Blog 
{% endblock %} 

{% block css_files %}
  <link rel="stylesheet" href="{% static "blog/post.css" %}" />
  <link rel="stylesheet" href="{% static "blog/index.css" %}" />
{% endblock %}

{% block content %}
<section id="welcome">
  <header>
    <img src="{% static "blog/images/.png" %}" alt="myblog" />
    <h2>BLOG</h2>
  </header>
  <p>Hi, I love to blog about tech and the world!</p>
</section>

<section id="latest-posts">
  <h2>My Latest Thoughts</h2>

  <ul>
    {% include "blog/includes/post.html" %}### HERE IS THE PROBLEM 
  </ul>
</section>

<section id="about">
  <h2>What I Do</h2>
  <p>
     programming is fun 
  </p>
  <p>
    hello this is  my blog 
  </p>
</section>
{% endblock %}

####  post.html  ####

{% extends "templates/base.html" %} 
{% load static %}


<li>
  <article class="post">
    <a href="{% url "post-detail-page" post.slug %}">
      <img src="{% static "blog/images/"|add:post.image %}" alt="{{ post.title }}"
      />
      <div class="post__content">
        <h3>{{ post.title }}</h3>
        <p>
          {{ post.excerpt }}
        </p>
      </div>
    </a>
  </article>
</li>


##########TRACEBACK############

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 3.2.11
Python Version: 3.6.9
Installed Applications:
['blog',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed 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']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    This engine did not provide a list of tried templates.


Template error:
In template /home/jo/My_site/templates/base.html, error at line 0
   blog/includes/post.html
   1 : {% load static %}
   2 : 
   3 : <!DOCTYPE html>
   4 : <html lang="en">
   5 : <head>
   6 :   <meta charset="UTF-8">
   7 :   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   8 :   <title>{% block title %}{% endblock %}</title>
   9 :   <link rel="stylesheet" href="{% static "app.css" %}">
   10 :   {% block css_files %}{% endblock %}


Traceback (most recent call last):
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/loader_tags.py", line 183, in render
    template = context.template.engine.select_template(template_name)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/engine.py", line 180, in select_template
    raise TemplateDoesNotExist(', '.join(not_found))

The above exception (blog/includes/post.html) was the direct cause of the following exception:
  File "/home/jo/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/jo/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/jo/My_site/blog/views.py", line 75, in starting_page
    <source code not available>
  File "/home/jo/.local/lib/python3.6/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 63, in render
    reraise(exc, self.backend)
  File "/home/jo/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 84, in reraise
    raise new from exc

Exception Type: TemplateDoesNotExist at /
Exception Value: blog/includes/post.html


###urls.py#####

from django.urls import path

from . import views

urlpatterns = [
    path("", views.starting_page, name="starting-page"),
    path("posts", views.posts, name="posts-page"),
    path("posts/<slug:slug>", views.post_detail,
         name="post-detail-page")  # /posts/my-first-post
]

###views.py####

from django.shortcuts import render

# Create your views here.

def starting_page(request):
  return render(request, "blog/index.html")

def posts(request):
  return render(request, "blog/all-posts.html")

def post_detail(request):
  pass


###  settings.py  ########
Django settings for My_site project.


INSTALLED_APPS = [
    "blog",
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]


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

######

【问题讨论】:

  • 问题出在你的模板目录中,你的目录 blog/include/ 中有 post.html 吗?
  • 可以分享模板的文件目录结构
  • 文件目录如下:blog/templates/blog/includes/post.html
  • 和博客/templates/博客/index.html
  • 在 index.html 中使用 {% include "includes/post.html" %}

标签: html python-3.x django templates django-templates


【解决方案1】:

尝试更改settings.py

TEMPLATES = [
    ...
    'DIRS': [str.format(BASE_DIR, "/templates")]
    ...

到这里:

TEMPLATES = [
    ...
    'DIRS': ["templates/"]
    ...

再试一次:

{% include "blog/includes/post.html" %}

【讨论】:

  • 这不起作用,它继续返回我上面列出的相同的引用,模板渲染时出错在模板/home/jo/My_site/templates/base.html中,错误在第 0 行 blog/includes/post.html
  • post.html 中删除{% extends "templates/base.html" %}。你要延长两次。
  • 我如你所说的那样删除了,我只是添加了扩展以查看这是否是问题,我也得到了这一行:上述异常(blog/includes/post.html)是直接原因以下异常:本地变量 /home/jo/My_site/blog/views.py,第 75 行,在starting_page ** **
猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2023-01-30
  • 2017-09-03
  • 1970-01-01
  • 2016-07-03
相关资源
最近更新 更多