【问题标题】:template folder in django not recognized digital oceandjango 中的模板文件夹无法识别数字海洋
【发布时间】:2016-11-16 09:24:03
【问题描述】:

我遇到的问题是这是我的项目树:

django_project
|-- circuitos
| |-- admin.py
| |-- apps.py
| |-- __init__.py
| |-- migrations
| | |-- 0001_initial.py
| | |-- 0002_auto_20161031_1107.py
| | |-- 0003_circuito_imagen_trazado.py
| | |-- 0004_circuito_fecha_evento.py
| | |-- 0005_auto_20161111_1025.py
| | |-- 0006_auto_20161111_1026.py
| | |-- 0007_auto_20161111_1028.py
| | |-- 0008_auto_20161111_1133.py
| | |-- __init__.py
| | `-- __pycache__
| | |-- 0001_initial.cpython-35.pyc
| | |-- 0002_auto_20161031_1107.cpython-35.pyc
| | |-- 0003_circuito_imagen_trazado.cpython-35.pyc
| | |-- 0004_circuito_fecha_evento.cpython-35.pyc
| | |-- 0005_auto_20161111_1025.cpython-35.pyc
| | |-- 0006_auto_20161111_1026.cpython-35.pyc
| | |-- 0007_auto_20161111_1028.cpython-35.pyc
| | |-- 0008_auto_20161111_1133.cpython-35.pyc
| | `-- __init__.cpython-35.pyc
| |-- models.py
| |-- __pycache__
| | |-- admin.cpython-35.pyc
| | |-- __init__.cpython-35.pyc
| | |-- models.cpython-35.pyc
| | |-- urls.cpython-35.pyc
| | `-- views.cpython-35.pyc
| |-- tests.py
| |-- urls.py
| `-- views.py
|-- django_project
| |-- __init__.py
| |-- __init__.pyc
| |-- settings.py
| |-- settings.pyc
| |-- settings.pye
| |-- urls.py
| |-- urls.pyc
| |-- wsgi.py
| `-- wsgi.pyc
|-- equipos
| |-- admin.py
| |-- apps.py
| |-- __init__.py
| |-- migrations
| | |-- 0001_initial.py
| | |-- 0002_remove_equipo_slug.py
| | |-- 0003_equipo_imagen_equipo.py
| | |-- __init__.py
| | `-- __pycache__
| | |-- 0001_initial.cpython-35.pyc
| | |-- 0002_remove_equipo_slug.cpython-35.pyc
| | |-- 0003_equipo_imagen_equipo.cpython-35.pyc
| | `-- __init__.cpython-35.pyc
| |-- models.py
| |-- __pycache__
| | |-- admin.cpython-35.pyc
| | |-- __init__.cpython-35.pyc
| | |-- models.cpython-35.pyc
| | |-- urls.cpython-35.pyc
| | `-- views.cpython-35.pyc
| |-- tests.py
| |-- urls.py
| `-- views.py
|-- manage.py
|-- media
|-- noticias
| |-- admin.py
| |-- apps.py
| |-- __init__.py
| |-- management
| | |-- commands
| | | |-- __init__.py
| | | |-- __init__.pyc
| | | |-- __pycache__
| | | | |-- __init__.cpython-35.pyc
| | | | `-- script.cpython-35.pyc
| | | `-- script.py
| | |-- __init__.py
| | |-- __init__.pyc
| | `-- __pycache__
| | `-- __init__.cpython-35.pyc
| |-- migrations
| | |-- 0001_initial.py
| | |-- 0002_noticia_fecha_noticia.py
| | |-- 0003_auto_20161111_1127.py
| | |-- 0004_auto_20161111_1133.py
| | |-- 0005_auto_20161111_1133.py
| | |-- __init__.py
| | `-- __pycache__
| | |-- 0001_initial.cpython-35.pyc
| | |-- 0002_noticia_fecha_noticia.cpython-35.pyc
| | |-- 0003_auto_20161111_1127.cpython-35.pyc
| | |-- 0004_auto_20161111_1133.cpython-35.pyc
| | |-- 0005_auto_20161111_1133.cpython-35.pyc
| | `-- __init__.cpython-35.pyc
| |-- models.py
| |-- __pycache__
| | |-- admin.cpython-35.pyc
| | |-- __init__.cpython-35.pyc
| | |-- models.cpython-35.pyc
| | |-- urls.cpython-35.pyc
| | `-- views.cpython-35.pyc
| |-- tests.py
| |-- urls.py
| `-- views.py
|-- pilotos
| |-- admin.py
| |-- apps.py
| |-- __init__.py
| |-- migrations
| | |-- 0001_initial.py
| | |-- 0002_auto_20161030_1237.py
| | |-- 0003_auto_20161111_1037.py
| | |-- 0004_auto_20161111_1133.py
| | |-- __init__.py
| | `-- __pycache__
| | |-- 0001_initial.cpython-35.pyc
| | |-- 0002_auto_20161030_1237.cpython-35.pyc
| | |-- 0003_auto_20161111_1037.cpython-35.pyc
| | |-- 0004_auto_20161111_1133.cpython-35.pyc
| | `-- __init__.cpython-35.pyc
| |-- models.py
| |-- __pycache__
| | |-- admin.cpython-35.pyc
| | |-- __init__.cpython-35.pyc
| | |-- models.cpython-35.pyc
| | |-- urls.cpython-35.pyc
| | `-- views.cpython-35.pyc
| |-- tests.py
| |-- urls.py
| `-- views.py
|-- static
| |-- css
| | `-- base.css
| `-- js
| `-- base.js
`-- templates
|-- base.html
|-- circuitos
| |-- circuito_confirm_delete.html
| |-- circuito_detail.html
| |-- circuito_form.html
| `-- circuito_list.html
|-- equipos
| |-- equipo_confirm_delete.html
| |-- equipo_detail.html
| |-- equipo_form.html
| `-- equipo_list.html
|-- noticias
| |-- noticia_confirm_delete.html
| |-- noticia_form.html
| `-- noticia_list.html
`-- pilotos
|-- piloto_confirm_delete.html
|-- piloto_detail.html
|-- piloto_form.html
`-- piloto_list.html

当我运行服务器时,对于应用程序“noticias”,服务器会显示以下错误:

TemplateDoesNotExist at /noticias/
noticias/noticia_list.html
Request Method: GET
Request URL:    http://178.62.112.135/noticias/
Django Version: 1.6.1
Exception Type: TemplateDoesNotExist
Exception Value:    
noticias/noticia_list.html
Exception Location: /usr/lib/python2.7/dist-packages/django/template/loader.py in select_template, line 186
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/home/django/django_project',
'/home/django',
'/usr/bin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']
Server time:    Wed, 16 Nov 2016 08:43:22 +0000

我在 manage.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',
            ],
        },
    },
]

但它不起作用。我该如何解决?

【问题讨论】:

  • 您的模板在模板文件夹中,而不是在noticias 文件夹中?你如何调用模板的文件夹?
  • @Mr.A 我删除了它,但它说的是一样的
  • 我尝试了很多东西,但没有任何效果。我的项目在 Django 1.10 中,而 Digital Ocean 有 Django 1.6。我从 Digital Ocean 更新了 django 的版本,但即使这样也不起作用。请问,有人可以帮我吗?

标签: python django django-templates digital-ocean


【解决方案1】:

看起来您的模板不在模板文件夹中。将模板“noticia_list.html”移动到以下目录“/home/django/django_project/templates/noticias”中。 现在您可以在带有前缀“noticias/”的代码中使用此模板。例如:

class MyView(ListView):
    template_name = 'noticias/noticia_list.html'

或者在另一个模板中:

    {% include 'noticias/noticia_list.html' %}

更新: 对于 Django 1.6 使用 TEMPLATE_DIRS 设置:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (os.path.join(ВASE_DIR, 'templates'),) 

【讨论】:

  • 嗨,正如你所说,我已经在模板文件夹中按模块获取了所有模板,但它现在正在工作:(
  • @Albert 您正在使用 Django 1.6。试试这个: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file))) TEMPLATE_DIRS = (os.path.join(ВASE_DIR, 'templates'), )
猜你喜欢
  • 2019-01-29
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
相关资源
最近更新 更多