【问题标题】:TemplateDoesNotExist | django 3.0.1 ,python 3.6模板不存在 | django 3.0.1,python 3.6
【发布时间】:2020-01-04 12:35:30
【问题描述】:

我是 Django 新手,我正在使用系统 Windows 10、Django 版本 3.0.1、python 3.6 我尝试使用为这个问题提供的所有相关解决方案,但不幸的是,这些解决方案都没有解决我的问题。 我在 myapp"index.html" 里面创建了一个名为 templates 的文件夹,我在我的 views 中写了几行代码.py 文件为

from django.shortcuts import render  
from django.http import HttpResponse
from django.template import loader 
def index(request):
    template = loader.get_template('index.html')
    return HttpResponse(template.render())

这是我的 settings.py

BASE_DIR = (os.path.dirname(os.path.abspath(__file__))



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

**回溯错误**

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

Using engine django:
    * django.template.loaders.filesystem.Loader: E:\djangoLearning\myproject\myapp\templates\index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: E:\djangoLearning\djangoenv\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: E:\djangoLearning\djangoenv\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: E:\djangoLearning\myproject\myapp\templates\index.html (Source does not exist)



Traceback (most recent call last):
  File "E:\djangoLearning\djangoenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "E:\djangoLearning\djangoenv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\djangoLearning\djangoenv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\djangoLearning\myproject\myapp\views.py", line 9, in index
    template = loader.get_template('index.html')
  File "E:\djangoLearning\djangoenv\lib\site-packages\django\template\loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /index/
Exception Value: index.html

工作目录

myproject
myapp
  templates
      index.html

【问题讨论】:

  • 您说您在myprojects 目录下创建了模板,但在您的工作目录上它位于myapp 下。但是如果在myapp下你可以试试template = loader.get_template('myapp/index.html')
  • 谢谢,我编辑了,其实templates目录只在myapp下

标签: django django-templates python-3.6


【解决方案1】:

步骤:-1) 转到您的项目 settings.py 文件

添加如下配置模板列表:

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

]

步骤:--2) 转到您的应用 >> 创建 templates 文件夹 >> 再次使用 your_app_name 创建一个文件夹 >> 创建您的模板文件,例如:index.html。

 Your_APP_Name
     |-templates
         |-Your_APP_Name
                |-index.html

假设您有一个博客应用程序。所以结构会是这样的:

 blog
     |-templates
         |-blog
                |-index.html

Step:-3) 现在您可以在 render 方法中精确调用 template 文件了。所以视图将如下所示:

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

   def index(request):
       return render(request,'your_app_name/index.html')

【讨论】:

  • 是的,您的解决方案有效,但为什么我们需要在模板目录下再次创建 myapp 目录?
  • @PrashantRajput,实际上这是用于 Django 模板搜索的方式。我们可以直接在 app_name/index.html 下使用。 Django 模板规则是它在项目中搜索模板文件夹,因此它将在哪里获得 index.html,它将假定 index.html。因为也可以为其他文件写入相同的模板文件名。因此,如果您再次创建具有相同名称的子目录,那么它将精确您的模板文件。其实你会在这里得到详细信息:docs.djangoproject.com/en/3.0/intro/tutorial03/…
猜你喜欢
  • 1970-01-01
  • 2017-01-23
  • 2010-12-27
  • 2021-01-07
  • 2015-12-10
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多