我的本地机器运行Python 2.5和Ubuntu 8.10上的Nginx, Django是根据最新的开发主干构建的。

对于我请求的每个URL,它抛出:

TemplateDoesNotExist at /appname/path appname/template_name.html Django尝试加载这些模板,顺序如下: 使用loader django.template.loaders. filessystem .function: 使用loader django.template.loaders.app_directories.function: TEMPLATE_DIRS (“/ usr / lib / python2.5 /网站/ projectname /模板”,)

在这种情况下,它是否在寻找/usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html ?奇怪的是这个文件确实存在于磁盘上。为什么Django找不到它?

我在Ubuntu 9.04上使用Python 2.6在远程服务器上运行相同的应用程序,没有出现这样的问题。其他设置相同。

在我的本地机器上是否有任何配置错误的地方,或者是什么可能导致了这样的错误,我应该调查一下?

在我的settings.py中,我指定了:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
# Find templates in the same folder as settings.py.
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)

它应该寻找以下文件:

/usr/lib/python2.5/site-packages projectname /模板/ appname1 / template1.html /usr/lib/python2.5/site-packages projectname /模板/ appname1 / template2.html /usr/lib/python2.5/site-packages projectname /模板/ appname2 / template3.html ...

以上所有文件都存在于磁盘上。

解决了

在我尝试之后,它现在工作了:

chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*

这是奇怪的。我不需要在远程服务器上执行此操作即可使其工作。


当前回答

Django 3的工作原理

我发现我相信好方法,我有base.html在根文件夹,所有其他html文件在应用程序文件夹,我 settings.py

import os

# This settings are to allow store templates,static and media files in root folder

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')

# This is default path from Django, must be added 
#AFTER our BASE_DIR otherwise DB will be broken.
BASE_DIR = Path(__file__).resolve().parent.parent

# add your apps to Installed apps
INSTALLED_APPS = [
    'main',
    'weblogin',
     ..........
]

# Now add TEMPLATE_DIR to  'DIRS' where in TEMPLATES like bellow
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, BASE_DIR,],
        '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',
            ],
        },
    },
]
# On end of Settings.py put this refferences to Static and Media files
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_URL = '/static/'

MEDIA_ROOT = [MEDIA_DIR,]
MEDIA_URL = '/media/'

如果你的数据库有问题,请检查你是否把原来的BASE_DIR下面的新BASE_DIR,否则改变

# Original
'NAME': BASE_DIR / 'db.sqlite3',
# to
'NAME': os.path.join(BASE_DIR,'db.sqlite3'),

Django现在可以在App文件夹和根文件夹中找到HTML和静态文件,而不需要在文件前添加App文件夹的名称。

Struture:
-DjangoProject
    -static(css,JS ...)
    -templates(base.html, ....)
    -other django files like (manage.py, ....)
    -App1
        -templates(index1.html, other html files can extend now base.html too)
        -other App1 files
    -App2
        -templates(index2.html, other html files can extend now base.html too)
        -other App2 files

其他回答

Django 3的工作原理

我发现我相信好方法,我有base.html在根文件夹,所有其他html文件在应用程序文件夹,我 settings.py

import os

# This settings are to allow store templates,static and media files in root folder

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')

# This is default path from Django, must be added 
#AFTER our BASE_DIR otherwise DB will be broken.
BASE_DIR = Path(__file__).resolve().parent.parent

# add your apps to Installed apps
INSTALLED_APPS = [
    'main',
    'weblogin',
     ..........
]

# Now add TEMPLATE_DIR to  'DIRS' where in TEMPLATES like bellow
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, BASE_DIR,],
        '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',
            ],
        },
    },
]
# On end of Settings.py put this refferences to Static and Media files
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_URL = '/static/'

MEDIA_ROOT = [MEDIA_DIR,]
MEDIA_URL = '/media/'

如果你的数据库有问题,请检查你是否把原来的BASE_DIR下面的新BASE_DIR,否则改变

# Original
'NAME': BASE_DIR / 'db.sqlite3',
# to
'NAME': os.path.join(BASE_DIR,'db.sqlite3'),

Django现在可以在App文件夹和根文件夹中找到HTML和静态文件,而不需要在文件前添加App文件夹的名称。

Struture:
-DjangoProject
    -static(css,JS ...)
    -templates(base.html, ....)
    -other django files like (manage.py, ....)
    -App1
        -templates(index1.html, other html files can extend now base.html too)
        -other App1 files
    -App2
        -templates(index2.html, other html files can extend now base.html too)
        -other App2 files

在设置.py时,删除TEMPLATE_LOADERS和TEMPLATE DIRS,然后添加

TEMPLATES = [
 {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['/home/jay/apijay/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、创建目录和模板文件。

假设你有一个名为mysite的项目,你想添加一个名为your_app_name的应用程序。将模板文件放在mysite/your_app_name/templates/your_app_name目录下,如下所示。

├── mysite
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── your_app_name
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── templates
│   │   └── your_app_name
│   │       └── my_index.html
│   ├── urls.py
│   └── views.py

2、添加你的应用到INSTALLED_APPS。

修改settings.py

INSTALLED_APPS = [
    ...
    'your_app_name',
    ...
]

3、将你的应用目录添加到TEMPLATES的DIRS中。

修改settings.py。

添加操作系统导入

import os
TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates'),
                 os.path.join(BASE_DIR, 'your_app_name', 'templates', 'your_app_name'),
                ...
                ]
    }
]

找到这个元组:

    import os
    SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

    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',
                ],
            },
        },
    ]

你需要添加'DIRS'字符串

os.path.join(BASE_DIR, 'templates')

所以你需要:

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

我不好意思承认这一点,但对我来说,问题是一个模板被指定为....HML代替....html。小心!