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

Docker部署Django应用出现TemplateDoesNotExist错误求助

Django容器部署后TemplateDoesNotExist问题排查

问题描述

本地运行正常的Django应用,通过Docker容器部署后服务可正常启动,但发起请求时出现TemplateDoesNotExist错误,模板文件位于Blog应用下的templates/blog/pages.html。

相关配置

Dockerfile代码

#syntax=docker/dockerfile:1
#FROM ubuntu:latest
FROM ubuntu:latest
FROM python:3.8-slim-buster
WORKDIR /app
#COPY requirements.txt requirements.txt

#RUN pip3 install -r requirements.txt
#
#CMD python . /manage.py runserver 0.0.0.0:8000"""
# pull the official base image

# set work directory


# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN apt-get update&& \
    apt-get -y install sudo
RUN apt-get install -y tesseract-ocr
RUN apt-get install -y build-essential libpoppler-cpp-dev pkg-config python-dev mupdf

RUN apt-get install ffmpeg libsm6 libxext6  -y

RUN apt-get install -y default-jre
RUN apt-get install -y default-jdk


RUN apt-get update
RUN apt-get install -y python3-pip
RUN pip3 install --upgrade pip
#RUN pip3 install virtualenv
#RUN python3 -m virtualenv env
#RUN source env/bin/activate
COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt
# copy project
COPY . /app

EXPOSE 8000

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml代码

version: "3"
   
services:
   web:
       build: .
       command: python3 manage.py runserver 0.0.0.0:8000
       ports:
           - 8000:8000

项目目录结构

Directory: E:\djangoproject\blogproject

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        22-12-2022     19:39                Blog
d-----        22-12-2022     19:39                Blogproject
d-----        22-12-2022     19:40                venv
-a----        22-12-2022     19:46         192512 db.sqlite3
-a----        21-12-2022     18:02            153 docker-compose.yml
-a----        22-12-2022     15:51           1097 Dockerfile
-a----        24-12-2021     13:00            689 manage.py
-a----        20-12-2022     13:09            578 requirements.txt

错误日志

TemplateDoesNotExist at /
blog/pages.html 
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 3.2.8
Exception Type: TemplateDoesNotExist
Exception Value:    
blog/pages.html 
Exception Location: /usr/local/lib/python3.8/site-packages/django/template/backends/django.py, line 84, in reraise
Python Executable:  /usr/local/bin/python3
Python Version: 3.8.16
Python Path:    
['/app',
 '/usr/local/lib/python38.zip',
 '/usr/local/lib/python3.8',
 '/usr/local/lib/python3.8/lib-dynload',
 '/usr/local/lib/python3.8/site-packages']
Server time:    Fri, 23 Dec 2022 05:53:42 +0000
Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

This engine did not provide a list of tried templates.
Error during template rendering
In template /app/Blog/templates/blog/base.html, error at line 0

排查解决建议

  • 检查Django模板配置:确保settings.py中TEMPLATES配置的APP_DIRS设为True,这样Django会自动扫描每个已安装应用下的templates目录。示例配置:

    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',
                ],
            },
        },
    ]
    
  • 确认应用已注册:检查settings.py的INSTALLED_APPS中是否包含Blog,只有注册过的应用,Django才会处理其模板目录:

    INSTALLED_APPS = [
        # 其他默认应用
        'Blog',
    ]
    
  • 验证容器内模板文件存在:进入运行中的容器,检查模板文件是否被正确复制:

    # 替换<容器ID>为实际容器ID,可通过docker ps获取
    docker exec -it <容器ID> ls /app/Blog/templates/blog/
    

    如果文件不存在,检查是否有.dockerignore文件排除了templates目录,若有则删除对应规则,重新构建容器。

  • 清理缓存并重新构建:执行以下命令清理旧容器和缓存,重新构建镜像并启动服务:

    docker-compose down -v
    docker-compose up --build
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 04:02:59