【问题标题】:Template Does not exist模板不存在
【发布时间】:2013-05-28 01:58:09
【问题描述】:

我是 Django 新手。我使用 pydev eclipse 作为 IDE。首先,我创建了一个项目,然后在该项目上创建了一个欢迎应用程序。我在项目中创建了一个名为 Templates 的文件夹,并创建了一个文件“home.html”并且 home.html 包含

<div>
This is my first site
</div> 

我将settings.py文件修改为

TEMPLATE_DIRS = ("Templates")

INSTALLED_APPS = (
    ..........#all default items
    'welcome', #the added one
)

views.py 包含

from django.shortcuts import render_to_response
def home(request):
    return render_to_response('home.html')

urls.py 包含

from django.conf.urls import patterns, include, url
from welcome.views import home
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'MajorProject.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^home/$', home),

)

然后我将它作为 django 项目运行并打开我的浏览器并在 localhost:8000/home 上查看 它显示错误

TemplateDoesNotExist at /home/
home.html
Request Method: GET
Request URL:    http://localhost:8000/home/
Django Version: 1.6
Exception Type: TemplateDoesNotExist
Exception Value:    
home.html
Exception Location: C:\Python27\django\template\loader.py in find_template, line 131
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2
Python Path:    
['D:\\Bishnu\\BE\\4th year\\8th semester\\Major Project II\\Working\\Workspace\\MajorProject',
 'C:\\Python27\\lib\\site-packages\\distribute-0.6.35-py2.7.egg',
 'D:\\Bishnu\\BE\\4th year\\8th semester\\Major Project II\\Working\\Workspace\\MajorProject',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode',
 'C:\\Windows\\SYSTEM32\\python27.zip']
Server time:    Sun, 2 Jun 2013 14:25:52 +0545

【问题讨论】:

  • 尝试使用绝对路径,而不是相对路径。

标签: django


【解决方案1】:

尝试在setting.py上设置模板目录。
作为

TEMPLATE_DIRS = (
                    os.path.join(os.path.dirname(__file__),'templates'),
)

【讨论】:

    【解决方案2】:

    如果你使用的是 Django 1.8+

    您将收到以下警告:

    (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS, TEMPLATE_DEBUG.
    

    将您的模板目录添加到 DIRS 字典下的 Base TEMPLATES 设置中

    像这样:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [
                root("templates"), #### Here ####
            ],
            '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',
                ],
            },
        },
    ]
    

    【讨论】:

    • 节省了我的时间。谢谢
    【解决方案3】:

    在 Django 1.9 中

    in settings.py
    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+r'\templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
        },
    },
    ]
    

    【讨论】:

      【解决方案4】:

      带有模板的目录应该命名为templates,而不是Templates(即使在Windows 上它可能是相同的)。还要确保您在PYTHONPATH 中有应用程序或您的项目和应用程序的正确目录结构,例如:

      project/
          project/
              settings.py
              ...
          welcome/
              templates/
                  home.html
              views.py
              ...
        manage.py
      

      那么您不需要更改TEMPLATE_DIRS,因为app_directories.Loader(默认启用)会在您的应用程序中找到模板。

      如果您仍想更改TEMPLATE_DIRS,请使用绝对路径,但首选方式是app_directories.Loader

      【讨论】:

        【解决方案5】:

        检查以下步骤进行修复

        第 1 步:

        在模板中,'DIRS' : [ ],提到这个:

        'DIRS': [os.path.join(BASE_DIR, 'templates')],

        第 2 步:

        进入你的views.py文件并寻找template_name(检查它的拼写,并检查你是否在这里提到了正确的html文件)

        第三步:

        检查您的 urls.py 文件是否在路径中提到了正确的模板名称

        格式:urlpatterns = [ path(" ", 类名或函数名, name = 模板名)

        【讨论】:

          【解决方案6】:

          BASE_DIR = 路径(文件).resolve().parent.parent

          这是 Django 的默认目录代码 看起来像这样 C:\user\pc_name\django_project

          但是如果你删除最后一个 .parent 它会是这样的 C:\user\pc_name\django_project\django_project

          所以新的 BASE_DIR 代码就是这个 BASE_DIR = 路径(文件).resolve().parent

          将此变量添加到 TEMPLATE_DIR TEMPLATE_DIR = os.path.join(BASE_DIR, 'template")

          最后一个代码定义了这个 C:\user\pc_name\django_project\django_project\template

          最终安全升级 DIRS 'DIRS': 'TEMPLATE_DIR'

          希望你做到了

          【讨论】:

          • 此答案格式不正确,可能难以阅读。请see here获取格式化帮助。
          猜你喜欢
          • 2014-03-10
          • 2017-01-23
          • 2010-12-27
          • 2011-08-06
          相关资源
          最近更新 更多