You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中TemplateDoesNotExist错误求助(/blog/createposts/路径)

Django TemplateDoesNotExist 错误解决

问题描述

访问路径/blog/createposts/时出现TemplateDoesNotExist错误,提示「No template names provided」,错误触发点在blog/templates/blog_post.html中的{% include rich_text_editor_component.html %}代码行。已尝试以下三种模板目录配置调整,但问题未解决:

  • 将DIRS设置为['templates']
  • 使用[TEMPLATE_DIR,]
  • 设置TEMPLATE_DIRS = ('os.path.join(BASE_DIR, "templates"),')

项目结构

amazonbackend/
├─ amazonbackend/
│  ├─ settings.py
│  ├─ urls.py
│  ├─ manage.py
├─ Homepage/
│  ├─ migrations
│  ├─ templates/
│  │  ├─ a.html
│  │  ├─ b.html
│  ├─ static
├─ Blog/
│  ├─ migrations
│  ├─ templates/
│  │  ├─ blog_post.html
│  │  ├─ rich_text_editor_component.html
│  ├─ static

相关代码

settings.py

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

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 = 'amazonbackend.urls'

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

blog/views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def BlogHomePage(request):
  return render(request, 'Blog_home.html')

def CreatePosts(request):
    return render(request, 'blog_post.html')

def MyPosts(request):
    return render(request,'myposts.html')

rich_text_editor_component.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rich Text Editor</title>
<style>
    .rich-text-editor {
        border: 1px solid #ccc;
        background-color: #fff;
    }

    .toolbar {
        background-color: #f0f0f0;
        padding: 5px;
    }

    .editor-content {
        padding: 10px;
    }

    .editor {
        width: 100%;
        min-height: 300px;
        border: 1px solid #ccc;
        padding: 5px;
    }
</style>
</head>
<body>
<div class="rich-text-editor">
    <div class="toolbar">
        <button onclick="execCommand('bold')">Bold</button>
        <button onclick="execCommand('italic')">Italic</button>
        <button onclick="execCommand('underline')">Underline</button>
    </div>
    <div class="editor-content">
        <div class="editor" contenteditable="true"></div>
    </div>
</div>

<script>
    function execCommand(command) {
        document.execCommand(command, false, null);
    }
</script>
</body>
</html>

blog_post.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="{% static 'css/blog_post.css' %}"/>
    <title>Create Posts</title>
</head>
<body>
    <div class="button-group">
        <button id="preview-button">Preview</button>
        <button id="save-draft-button">Save as Draft</button>
        <button id="publish-button">Publish</button>
    </div>
    <div class="title-bar">
        <input type="text" id="post-title" placeholder="Enter post title">
    </div>
    <div class="rich-toolbar">
        {% include rich_text_editor_component.html %}
    </div>
    <div class="content-container">
        <textarea id="post-content" placeholder="Write your post content here"></textarea>
    </div>
</body>
</html>

解决方案

核心问题:include标签语法错误

Django模板的{% include %}标签要求模板名必须是字符串,需要用引号包裹。当前代码中未给模板名加引号,Django会把rich_text_editor_component.html当成变量解析,导致找不到有效模板名,触发「No template names provided」错误。

修改blog_post.html中的include代码:

{% include "rich_text_editor_component.html" %}

额外说明

你的settings.py中TEMPLATES配置的APP_DIRS = True,Django会自动扫描每个已安装应用内的templates目录,所以Blog/templates/下的模板本来就能被识别,之前调整DIRS的操作未解决根本问题。

内容的提问来源于stack exchange,提问作者osama aslam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 07:27:33