Django 找不到模板目录
Django can't find template directory
我知道有很多类似的问题,但是none已经解决了我的问题。
Python版本:3.4
Django 版本:1.8
我在加载 http://127.0.0.1:8000/lfstd/.
时在 /lfstd/ 得到 TemplateDoesNotExist
system.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
lfstd/views.py:
def index(request):
[...]
return render(request, 'lfstd/index.html', context_dict)
项目urls.py:
from django.conf.urls import include, url
from django.contrib import admin
url(r'^admin/', include(admin.site.urls)),
url(r'^lfstd/', include('lfstd.urls')),
lfstd urls.py:
from django.conf.urls import patterns, url
from lfstd import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'))
运行 在 base_dir 和 template_path 上打印,我得到:
Base dir: C:\Users\Phil\PycharmProjects\TownBuddies
Template path: C:\Users\Phil\PycharmProjects\TownBuddies\templates
这正是我的项目和模板文件夹所在的位置。但是,django 不会在该文件夹中查找模板。它在以下文件夹中查找:
C:\Python34\lib\site-packages\django\contrib\admin\templates\lfstd\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\lfstd\index.html (File does not exist)
事实上,如果我将我的模板移到那里,它确实会找到它并且有效。
非常感谢任何帮助,我刚开始使用 Django 并完全卡住了...
编辑:
我将 TEMPLATE_DIRS 更改为 TEMPLATES 但 Django 仍然没有在模板文件夹中查找:
TEMPLATE_DIRS
设置在 django 1.8 中已弃用。您应该改用 TEMPLATES
:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_PATH],
'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',
],
},
},
]
我正在用 pytest-django 做一些单元测试,突然我重复出现完全出乎意料的 ** TemplateDoesNotExist** 错误。
我在试图弄清楚发生了什么时发现了这个 Whosebug 问题。
我终于通过删除所有 __pycache__
目录解决了这个问题。从那以后我了解到,当事情突然变得莫名其妙地不稳定时,在尝试修复它们之前清除 __pycache__
!
我使用此脚本递归清除了所有 __pycache__
目录,其中包含以下模块:
#!/usr/bin/env python
import os
import shutil
#!/usr/bin/env python
import os
import shutil
BASE_DIR = os.path.abspath(__file__)
print(BASE_DIR)
for root, dirs, files in os.walk(BASE_DIR):
for directory in dirs:
if directory == '__pycache__':
shutil.rmtree(os.path.join(root, directory))
我知道有很多类似的问题,但是none已经解决了我的问题。
Python版本:3.4 Django 版本:1.8
我在加载 http://127.0.0.1:8000/lfstd/.
时在 /lfstd/ 得到 TemplateDoesNotExistsystem.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
lfstd/views.py:
def index(request):
[...]
return render(request, 'lfstd/index.html', context_dict)
项目urls.py:
from django.conf.urls import include, url
from django.contrib import admin
url(r'^admin/', include(admin.site.urls)),
url(r'^lfstd/', include('lfstd.urls')),
lfstd urls.py:
from django.conf.urls import patterns, url
from lfstd import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'))
运行 在 base_dir 和 template_path 上打印,我得到:
Base dir: C:\Users\Phil\PycharmProjects\TownBuddies
Template path: C:\Users\Phil\PycharmProjects\TownBuddies\templates
这正是我的项目和模板文件夹所在的位置。但是,django 不会在该文件夹中查找模板。它在以下文件夹中查找:
C:\Python34\lib\site-packages\django\contrib\admin\templates\lfstd\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\lfstd\index.html (File does not exist)
事实上,如果我将我的模板移到那里,它确实会找到它并且有效。
非常感谢任何帮助,我刚开始使用 Django 并完全卡住了...
编辑:
我将 TEMPLATE_DIRS 更改为 TEMPLATES 但 Django 仍然没有在模板文件夹中查找:
TEMPLATE_DIRS
设置在 django 1.8 中已弃用。您应该改用 TEMPLATES
:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_PATH],
'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',
],
},
},
]
我正在用 pytest-django 做一些单元测试,突然我重复出现完全出乎意料的 ** TemplateDoesNotExist** 错误。
我在试图弄清楚发生了什么时发现了这个 Whosebug 问题。
我终于通过删除所有 __pycache__
目录解决了这个问题。从那以后我了解到,当事情突然变得莫名其妙地不稳定时,在尝试修复它们之前清除 __pycache__
!
我使用此脚本递归清除了所有 __pycache__
目录,其中包含以下模块:
#!/usr/bin/env python
import os
import shutil
#!/usr/bin/env python
import os
import shutil
BASE_DIR = os.path.abspath(__file__)
print(BASE_DIR)
for root, dirs, files in os.walk(BASE_DIR):
for directory in dirs:
if directory == '__pycache__':
shutil.rmtree(os.path.join(root, directory))