【问题标题】:Django: TemplateDoesNotExist at / home.html in my projectDjango: TemplateDoesNotExist at / home.html 在我的项目中
【发布时间】:2021-08-30 18:55:41
【问题描述】:

我不明白为什么 django 不能搜索 'home.html'

我的项目名称是lucifer

这是我的项目树

│   ├── lucifer
│   │   ├── __init__.py
│   │   ├── settings
│   │   │   ├── __init__.py
│   │   │   ├── development.py
│   │   │   ├── partials
│   │   │   │   ├── __init__.py
│   │   │   │   ├── base.py
│   │   │   │   ├── database.py
│   │   │   │   └── static.py
│   │   │   └── production.py
│   │   ├── templates
│   │   │   ├── base.html
│   │   │   ├── home.html
│   │   │   └── partials
│   │   │       ├── footer.html
│   │   │       └── header.html
│   │   ├── urls.py
│   │   ├── views
│   │   │   ├── __init__.py
│   │   │   └── home.py

还有我的home.html

{% extends 'base.html' %}

{% block title %}
Home
{% endblock %}

header.html

<p>it is header</p>

footer.html

<p>it is footer</p>

base.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>루시퍼 |{% block title %}{% endblock %}</title>
</head>
<body>

    {% include 'partials/header.html' %}

    {% block content %}
    {% endblock %}

    {% include 'partials/footer.html' %}

</body>
</html>

home.py

from django.views.generic import TemplateView


class Home(TemplateView):
    template_name = 'home.html'

urls.py

from django.conf.urls import url
from django.contrib import admin

from .views import *

urlpatterns = [
    url(r'^admin/', admin.site.urls),
url(r'$^', Home.as_view(), name='home'),
]

我觉得没问题..

但错误是

Django version 1.9.7, using settings 'lucifer.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /
Traceback (most recent call last):
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/core/handlers/base.py", line 174, in get_response
response = self.process_exception_by_middleware(e, request)
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/core/handlers/base.py", line 172, in get_response
response = response.render()
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/template/response.py", line 160, in render
self.content = self.rendered_content
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/template/response.py", line 135, in rendered_content
template = self._resolve_template(self.template_name)
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/template/response.py", line 90, in _resolve_template
new_template = self.resolve_template(template)
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/template/response.py", line 80, in resolve_template
return select_template(template, using=self.using)
  File "/Users/hanminsoo/.pyenv/versions/lucifer/lib/python3.4/site-packages/django/template/loader.py", line 74, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: home.html
[06/Feb/2017 07:40:34] "GET / HTTP/1.1" 500 81753

请给我一些建议谢谢

在 settings.partials.base.py 中添加 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',
            ],
        },
    },
]

【问题讨论】:

  • 你能发布你的TEMPLATES var吗?
  • @arcegk 嗯.. 抱歉,我无法理解您的评论 什么是 Templates var?如果是可变模板代码,我会在我的问题中插入此代码
  • @MinHan,模板配置应该在您的设置文件中。如果您使用的是 Mac 或 Unix,请尝试运行 grep TEMPLATES,或者手动查看文件。
  • 我的意思是TEMPLATES 变量位于您的设置文件之一
  • 你能发布整个项目结构吗?如果 lucifer 是一个子应用程序,django 不会选择模板。

标签: django django-templates


【解决方案1】:

请务必将lucifer 放入您的settings.pyINSTALLED_APPS APPS_DIR=True 将告诉 Django look inside your installed apps

【讨论】:

  • 这是正确的做事方式。将模板目录添加到 TEMPLATES 设置的其他答案将起作用,但属于反模式。
【解决方案2】:

这就是我们为我目前的项目所做的...

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

                # Enable {{ STATIC_URL }} and {{ MEDIA_URL }}
                'django.template.context_processors.media',
                'django.template.context_processors.static',
            ],
        },
    },
]

【讨论】:

    【解决方案3】:

    试试这个:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
            'APP_DIRS': '',
            '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',
                    'django.template.context_processors.media',
                    'django.template.context_processors.static',
                ],
                'loaders':[
                    'django.template.loaders.filesystem.Loader',
                ]
    
            },
    
        },
    ]
    

    【讨论】:

      【解决方案4】:
      'DIRS': [os.path.join(dirname(dirname(__file__)), '..')+'/templates']
      

      在模板[]中添加这个

      【讨论】:

        【解决方案5】:

        嘿,将模板文件夹添加到主项目目录:

        ├── calc
        │   ├── admin.py
        │   ├── apps.py
        │   ├── __init__.py
        │   ├── migrations
        │   │   └── __init__.py
        │   ├── models.py
        │   ├── __pycache__
        │   │   ├── __init__.cpython-37.pyc
        │   │   ├── urls.cpython-37.pyc
        │   │   └── views.cpython-37.pyc
        │   ├── tests.py
        │   ├── urls.py
        │   └── views.py
        ├── db.sqlite3
        ├── first_django
        │   ├── asgi.py
        │   ├── __init__.py
        │   ├── __pycache__
        │   │   ├── __init__.cpython-37.pyc
        │   │   ├── settings.cpython-37.pyc
        │   │   ├── urls.cpython-37.pyc
        │   │   └── wsgi.cpython-37.pyc
        │   ├── settings.py
        │   ├── urls.py
        │   └── wsgi.py
        ├── manage.py
        └── **templates**
            └── home.html
        

        【讨论】:

          【解决方案6】:

          导入操作系统并从设置中定义模板目录中的目录。它有效。

          'DIRS': [os.path.join(BASE_DIR, 'templates')],

          【讨论】:

            猜你喜欢
            • 2019-06-01
            • 2022-01-11
            • 2013-07-29
            • 2018-09-29
            • 1970-01-01
            • 1970-01-01
            • 2023-03-27
            • 1970-01-01
            • 2014-07-05
            相关资源
            最近更新 更多