[英]Django can't find template
我知道很多人都問過這個問題,但是盡管硬編碼到我的模板目錄的路徑,我似乎無法讓Django找到我的模板。
這是settings.py
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#django.template.loaders.eggs.Loader',
)
TEMPLATE_DIRS = (
"/Users/full/path/to/marketing_site/templates",
)
這是views.py文件:
def static (request, feature_page):
# this will get the appropriate feature page template.
template = loader.get_template('features/pricing.html')
c = RequestContext(request,{
})
return HttpResponse(template.render(c))
主文件夾內是templates文件夾和app文件夾。 我用來將應用程序放在與settings.py相同的文件夾中,但是看起來django 1.4已經更改了默認的文件結構。
我的錯誤是:
TemplateDoesNotExist at /features/pricing
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/auth/templates/feature_pricing.html (File does not exist)
更新:
我的網頁日志將TEMPLATE_DIRS列為()。
如果我在TEMPLATE_DIRS的settings.py頁面上放置一個print語句,我會正確地得到TEMPLATE_DIRS的打印輸出......所以不知道TEMPLATE_DIRS是否被使用(從它的外觀看)
我在settings.py中添加了額外的TEMPLATE_DIR
:(
最好為路徑變量設置相對路徑。 您可以這樣設置:
import os
PATH_PROJECT = os.path.realpath(os.path.dirname(__file__))
...
...
TEMPLATE_DIRS = (
PATH_PROJECT + '/templates/'
)
假設您正在使用Windows,您可能想嘗試:
TEMPLATE_DIRS = (
"C:/Users/full/path/to/marketing_site/templates",
)
我打賭/Users/full/path/to/marketing_site/templates
不包含features
目錄,或/Users/full/path/to/marketing_site/templates/features
不包含pricing.html
文件。
根據您的評論,看起來您正在調用loader.get_template('feature_pricing.html')
,而不是使用'feature/pricing.html'
。
編輯:我之前沒有注意到這一點:
主文件夾內是templates文件夾和app文件夾。 我用來將應用程序放在與settings.py相同的文件夾中,但是看起來django 1.4已經更改了默認的文件結構。
這可能導致問題。 更改目錄結構以匹配1.4 Django約定。 您可以只重新創建項目,將相關設置復制到新創建的settings.py中,然后復制所有文件。
嘗試將項目路徑添加到django.wsgi
import os
import sys
paths = ('path/to/project/',
'path/to/more /included/directories/',
)
for path in paths:
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.