Django / Python应用程序中的TemplateDoesNotExist

时间:2017-10-01 20:33:26

标签: python django pycharm

我正在尝试使用PyCharm运行Django应用程序并收到此错误:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.7.1
Python Version: 2.7.14
Installed Applications:
('django_admin_bootstrapped',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'compressor',
'menu',
'django_extensions',
'estabelecimento',
'bootstrap3',
'django_autocomplete',
'easy_select2',
'daterange_filter',
'qrcode')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Users\Marcos\PycharmProjects\japedi\django_admin_bootstrapped\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\admin\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\rest_framework\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\compressor\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\django_extensions\templates\index.html (File does not exist)
C:\Users\Marcos\PycharmProjects\japedi\bootstrap3\templates\index.html (File does not exist)
C:\Users\Marcos\PycharmProjects\japedi\daterange_filter\templates\index.html (File does not exist)
C:\Users\Marcos\PycharmProjects\japedi\qrcode\templates\index.html (File does not exist)



Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
137.                 response = response.render()
File "C:\Python27\lib\site-packages\django\template\response.py" in render
103.             self.content = self.rendered_content
File "C:\Python27\lib\site-packages\django\template\response.py" in rendered_content
78.         template = self.resolve_template(self.template_name)
File "C:\Python27\lib\site-packages\django\template\response.py" in resolve_template
54.             return loader.select_template(template)
File "C:\Python27\lib\site-packages\django\template\loader.py" in select_template
194.     raise TemplateDoesNotExist(', '.join(not_found))

Exception Type: TemplateDoesNotExist at /
Exception Value: index.html

项目中有一些名为模板的导演。对于每个特定的应用程序,其中都有一个文件夹模板。有谁可以帮我这个?生产环境中有一个版本,Linux在数字海洋中运行良好。我正在尝试使用Windows 10在我的机器上本地运行。

我尝试将DIRS放在settings.py中,但是没有逗号和" ="而不是":",没有成功。我的INSTALLED_APPS部分是这样的:

INSTALLED_APPS = (
    'django_admin_bootstrapped',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'compressor',
    'menu',
    'django_extensions',
    'estabelecimento',
    'bootstrap3',
    'django_autocomplete',
    'easy_select2',
    'daterange_filter',
    'qrcode'
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'grappelli2/templates'),
)

我是否需要在根文件夹中安装index.html文件?因为我只有一个index.wsgi。

Project Structure and views.py

1 个答案:

答案 0 :(得分:0)

有些事情可能导致这种情况。

  1. 如果您尝试加载的html文件位于项目根目录中的模板文件夹中,则需要确保找到它。
  2. 转到settings.py,TEMPLATES部分,然后按如下方式设置'DIRS'。

    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    
    1. 如果您想要的文件位于应用程序的模板目录中,那么 确保该应用位于settings.py
    2. 的INSTALLED_APPS部分

      这个最终的想法是一个长镜头,不应该是一个问题,但如果上述两个都是正确的,试试这个。

      我注意到,如果在INSTALLED_APPS列表中的最后一个应用程序之后没有逗号,我可以在PyCharm中收到此错误。

      奇怪的是,如果我添加了逗号并让应用程序运行,我就可以删除逗号,它仍然有用。

      我认为这种情况不会发生,但如果其他所有方法都失败了,请看看。

      -Richard