简体   繁体   English

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

[英]template folder in django not recognized digital ocean

the problem that I have is that this is my project tree: 我的问题是这是我的项目树:

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

And when I run the server, for the app "noticias" the server says an error of: 当我运行服务器时,对于应用程序“ 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

I added to the manage.py file the following code: 我将以下代码添加到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',
            ],
        },
    },
]

But it's not working. 但这不起作用。 How I can fix it? 我该如何解决?

Looks like your templates are not in the templates folder. 看起来您的模板不在模板文件夹中。 Move template 'noticia_list.html' into following directory '/home/django/django_project/templates/noticias'. 将模板“ noticia_list.html”移动到以下目录“ / home / django / django_project / templates / noticias”。 Now you can use this template in code with prefix 'noticias/'. 现在,您可以在代码中以前缀“ noticias /”使用此模板。 For example: 例如:

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

Or in another template: 或在另一个模板中:

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

UPD: for Django 1.6 use TEMPLATE_DIRS setting: 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'),) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM