【问题标题】:Django, template cannot be foundDjango,找不到模板
【发布时间】: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 设置都会有所帮助。

标签: django django-templates


【解决方案1】:

您的模板名称包含 abslute 路径 /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')

【讨论】:

  • 啊,这么小的东西。谢谢拯救了我的一天:)
【解决方案2】:

尝试在 /webapp/urls.py 上更改您的网址格式:

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

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

【讨论】:

  • 但是我已经在/urls.py中添加了该部分,因此将其添加到/webapp/urls.py会导致它找不到url。
猜你喜欢
  • 2018-01-02
  • 2012-07-10
  • 2015-03-14
  • 2020-05-27
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
相关资源
最近更新 更多