django中的模板文件夹无法识别数字海洋

时间:2016-11-16 09:24:03

标签: python django django-templates digital-ocean

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

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

当我运行服务器时,对于app" 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',
            ],
        },
    },
]

但它没有用。我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

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

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

或在另一个模板中:

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

UPD: 对于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'),) 
相关问题