由于某种原因,我创建的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:
答案 0 :(得分:5)
您的模板名称包含 aboslute 路径/webapp/list.html
。尝试将其设为相对路径webapp/list.html
,以便os.path.join(.)
按预期处理路径连接。
试试这个:
def profile_list(request):
return object_list(request,
queryset=Profile.objects.all(),
template_name='webapp/list.html', # removed slash here.
template_object_name='profiles')
答案 1 :(得分:0)
尝试从 /webapp/urls.py 更改您的网址格式:
url(r'^list/$', views.profile_list, name='profile_list'),
到
url(r'^webapp/list/$', views.profile_list, name='profile_list'),