sta*_*orn 1 django django-templates
由于某种原因,我创建的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)
Run Code Online (Sandbox Code Playgroud)
/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')
Run Code Online (Sandbox Code Playgroud)
/webapp/urls.py
from django.conf.urls.defaults import *
import views
urlpatterns = patterns('',
url(r'^list/$', views.profile_list, name='profile_list'),
)
Run Code Online (Sandbox Code Playgroud)
/urls.py
from django.conf.urls import *
urlpatterns = patterns('',
(r'^webapp/', include('webapp.urls')),
)
Run Code Online (Sandbox Code Playgroud)
/templates/webapp/list.html
<html>
<body>
Test
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
错误信息
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:
Run Code Online (Sandbox Code Playgroud)
您的模板名称包含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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2229 次 |
| 最近记录: |