【发布时间】:2012-01-12 18:28:24
【问题描述】:
由于某种原因,我创建的 webapp 找不到模板。我还尝试将 html 文件放在几个不同的位置,但仍然找不到。到目前为止,我已将模板导向器放在以下位置C:\xampp\htdocs\django_proj\templates\。其他人知道问题可能是什么吗?
我在这个 django 项目中有另一个 webapp。模型和视图看起来几乎一样,但是 webapp 可以找到模板。
/webapp/models.py
from django.db import models
class Profile(models.Model):
name = models.CharField(max_length=255)
age = models.IntegerField()
added_at = models.DateTimeField(auto_now_add=True)
/webapp/views.py
from django.views.generic.list_detail import object_list
from models import Profile
def profile_list(request):
return object_list(request,
queryset=Profile.objects.all(),
template_name='/webapp/list.html',
template_object_name='profiles')
/webapp/urls.py
from django.conf.urls.defaults import *
import views
urlpatterns = patterns('',
url(r'^list/$', views.profile_list, name='profile_list'),
)
/urls.py
from django.conf.urls import *
urlpatterns = patterns('',
(r'^webapp/', include('webapp.urls')),
)
/templates/webapp/list.html
<html>
<body>
Test
</body>
</html>
错误信息
TemplateDoesNotExist at /webapp/list/
/webapp/list.html
Request Method: GET
Request URL: http://localhost:8000/webapp/list/
Django Version: 1.4 pre-alpha
Exception Type: TemplateDoesNotExist
Exception Value:
/webapp/list.html
Exception Location: c:\tools\python27\lib\site-packages\django\template\loader.py in find_template, line 138
Python Executable: c:\tools\python27\python.exe
Python Version: 2.7.2
Python Path:
['C:\\xampp\\htdocs\\webnotes',
'c:\\tools\\python27\\lib\\site-packages\\pip-1.0.2-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'c:\\tools\\python27\\DLLs',
'c:\\tools\\python27\\lib',
'c:\\tools\\python27\\lib\\plat-win',
'c:\\tools\\python27\\lib\\lib-tk',
'c:\\tools\\python27',
'c:\\tools\\python27\\lib\\site-packages']
Server time: Thu, 12 Jan 2012 19:33:48 +0100
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
【问题讨论】:
-
您是否收到带有“找不到模板错误”的错误页面?您可以发布错误消息吗?它通常包含 Django 查找模板的路径列表。此外,任何相关的 Django 设置都会有所帮助。