我的本地机器运行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/*

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


当前回答

找到这个元组:

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

其他回答

在模板:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [r'C:\Users\islam\Desktop\html_project\django\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',
        ],
    },
},

]

将模板文件的完整目录放在视图中:

def home(request):
return render(request, "home\index.html")

启动模板后面的HTML文件路径

简而言之:

完整路径为: django C:\Users\islam\Desktop\ html_project \ \ templates \ \ index . html

django C:\Users\islam\Desktop\ html_project \ \模板 模板文件的完整路径将在TEMPLATES ` DIRS': [' '] Home \index.html模板后面的路径将在render()中显示

大家好,我找到了一个新的解决方案。实际上,它是在另一个模板中定义的,所以不要自己定义TEMPLATE_DIRS,而是将目录路径名称放在它们的位置:

1.创建一个文件夹“模板”在你的“应用程序”(让你这样命名你的应用程序) 你可以把HTML文件放在这里。 但强烈建议在“templates”文件夹中创建一个同名的文件夹(“app”),然后将html放在那里。进入'app/templates/app'文件夹

2.现在在'app'的urls.py中放入:

  path('', views.index, name='index'), # in case of  use default server index.html 

3.在'app'的views.py中输入:

from django.shortcuts import render 

def index(request): return
    render(request,"app/index.html")
    # name 'index' as you want

检查模板和appname目录的权限,使用ls -l或尝试从django执行绝对路径open()。

我想到了这个问题。下面是我解决这个问题的方法:

查看settings.py,找到TEMPLATES变量, 在TEMPLATES中,在DIRS列表中添加你的模板路径。对我来说,首先我将模板路径设置为TEMPLATES_PATH = os.path.join(BASE_DIR,'templates'),然后将TEMPLATES_PATH添加到DIRS列表,' DIRS':[TEMPLATES_PATH,]。 然后重新启动服务器,TemplateDoesNotExist异常消失。 就是这样。