我知道很多人都问过这个问题,但是尽管硬编码到我的模板目录的路径,我似乎无法让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",
)
Run Code Online (Sandbox Code Playgroud)
这是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))
Run Code Online (Sandbox Code Playgroud)
主文件夹内是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)
Run Code Online (Sandbox Code Playgroud)
更新:
我的网页日志将TEMPLATE_DIRS列为().
如果我在TEMPLATE_DIRS的settings.py页面上放置一个print语句,我会正确地得到TEMPLATE_DIRS的打印输出......所以不知道TEMPLATE_DIRS是否被使用(从它的外观看)
我正在通过这里找到的官方django 1.7教程.除了django找不到应用程序的模板外,一切都很顺利.它找到我放在workspace/mysite/templates下但不在workspace/mysite/polls/templates下的模板.
workspace是我在我的主目录中的一个文件夹,我保留了所有的web项目.
我的路径是〜工作区/ mysite /
而项目结构是
`workspace
|
mysite
|
db.sqlite3 - manage.py - mysite - mysite_env - polls - templates`
Run Code Online (Sandbox Code Playgroud)
为简洁起见,我只列出每个文件夹的内容:
在我指定的mysite/settings.py中
`TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]`
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个路径到polls/templates文件夹,但这也没有用.
index的polls/views.py设置是:
`def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('/polls/index.html')
context = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))`
Run Code Online (Sandbox Code Playgroud)
实际的polls/templates/polls/index.html包含:
`{% if latest_question_list % }
<ul>
{% for question in latest_question_list %} …Run Code Online (Sandbox Code Playgroud)