你好,祝你愉快,
我现在正在上Django课程,在那里我建立了一个博客。
我一直得到一个:
TemplateDoesNotExist at /blog/include/post.html
我似乎解决不了。
但我认为这与{%包括%}模板标记有关
我有两个文件:1)post.html和2)index.html
我正在尝试在index.html中包含%}%}
但当我这么做的时候我就明白了:
TemplateDoesNotExist at /blog/include/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" %}我试过所有的路,但都没有用。
使其工作的唯一方法是不使用{%包含%},而是直接复制和粘贴post.html,但这是不切实际的,不允许我继续我的项目
这是目录结构:
博客/模板/博客/包含/ post.html
博客/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',
],
},
},
]
######发布于 2022-02-02 17:13:42
尝试在settings.py中更改
TEMPLATES = [
...
'DIRS': [str.format(BASE_DIR, "/templates")]
...对此:
TEMPLATES = [
...
'DIRS': ["templates/"]
...再试一次:
{% include "blog/includes/post.html" %}https://stackoverflow.com/questions/70959488
复制相似问题