2012-01-12 149 views
1

出于某种原因,我创建的webapp无法找到模板。我也尝试将html文件放置到多个不同的位置,但它仍然无法找到它。截至目前,我已将我的模板总监放在以下位置C:\xampp\htdocs\django_proj\templates\。别人是否知道问题可能是什么。Django,无法找到模板

我在这个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

你得到一个错误页的“模板未找到错误”?你能发布错误信息吗?它通常包含Django寻找模板的路径列表。此外,任何相关的Django设置都会有所帮助。 – 2012-01-12 18:31:38

+0

当然我现在要添加它 – starcorn 2012-01-12 18:34:10

回答

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') 
+0

+1也打我吧:-) – 2012-01-12 18:44:33

+0

啊,这么小的一件事。谢谢救了我的一天:) – starcorn 2012-01-12 18:46:18

0

尝试改变/web应用程序的URL模式/ urls.py from:

url(r'^list/$', views.profile_list, name='profile_list'), 

url(r'^webapp/list/$', views.profile_list, name='profile_list'), 
+0

但是我已经在'/ urls.py'中添加了该部分,因此将它添加到'/ webapp/urls.py'中会导致它找不到该url。 – starcorn 2012-01-12 18:42:06