【问题标题】:TemplateDoesNotExist. loading static files and templates Django project structure模板不存在。加载静态文件和模板 Django 项目结构
【发布时间】:2015-02-05 17:25:19
【问题描述】:

我对如何从其他应用生成模板有点困惑

这是我的项目结构的图片:

我收到此错误:

TemplateDoesNotExist at /
home.html

home/views.py:

class HomeView(generic.TemplateView):
    template_name = "home.html"

Settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    os.path.join(BASE_DIR, "home/static/home/js"),
    os.path.join(BASE_DIR, "home/static/home/images"),
    os.path.join(BASE_DIR, "home/static/home/css"),
)


TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

项目/home/urls.py:

from django.conf.urls import patterns, url

from home.views import HomeView

urlpatterns = patterns('',
    url(r'^$', HomeView.as_view(), name="home"),
)

项目/urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include("home.urls", namespace="home")),

)

项目/模板/home/index.html

<!DOCTYPE html>
<html>
<head lang="en">
  ...
  <!-- Fonts -->
  <link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
  <!-- Custom CSS -->
  <link href="{{ STATIC_URL }}boothie.css" rel="stylesheet">
  <!-- Custom JS-->
  <script src="{{ STATIC_URL }}boothie.js"></script>

  <title>Boothie</title>
</head>
<body>
<div class="container">
   ...
</div>
</head>
  {% block content %}{% endblock %}
</body>
</html>

project/home/templates/home/home.html:

{% extends index.html %}

{% block content %}
    ...
    <script src="{{ STATIC_URL }}js/jquery.easing.1.3.js"></script>
    <script src="{{ STATIC_URL }}js/boothie.js"></script>
{% endblock %}

【问题讨论】:

  • 除非是拼写错误,否则您的模板将称为 home/index.html 而不是 home.html
  • @BurhanKhalid 嗯,我的 home 应用程序中有一个名为 home.html 的文件。我想我的命名约定不好。在我的项目/模板中,有一个名为 home 的目录,其中包含一个文件 index.html
  • home/templates/home.html吗?
  • @BurhanKhalid 我已经上传了我的项目结构的图片

标签: python django


【解决方案1】:

Django 在找到与请求的文件名匹配的第一个模板处停止,它将从 app 文件夹中的 templates 目录开始。

如果它在这里没有找到它,它将沿着链向上直到它搜索了TEMPLATE_DIRS 设置中的所有位置,然后它才会打印一个找不到模板的错误。它搜索模板的方式由TEMPLATE_LOADERS setting 定义。

在您的项目布局中,您想要的模板位于主应用程序的模板目录中,并且在此目录中,在home 目录下。因此,您认为模板路径应该是home/home.html

class HomeView(generic.TemplateView):
    template_name = "home/home.html"

【讨论】:

  • 我做了一些更改并添加了这个TEMPLATE_DIRS = ( os.path.join(BASE_DIR, 'templates/index'), os.path.join(BASE_DIR, 'home/templates/home'), ) 不确定这是否是一个好习惯 =/
  • 具体会增加搜索时间吗?
  • 只有在您拥有不属于某个应用的模板时才使用 TEMPLATE_DIRS。就个人而言,我从未发现需要此设置,因为我的所有模板都位于应用程序中。我真的不知道你的意思是“增加搜索时间”。
【解决方案2】:

如果您的模板目录位于项目的根目录中,则必须将其添加到 setitngs.py

如果它是应用程序的子目录(即在 settings.py 的 INSTALLED_APPS 中),那么应用程序模板加载器(默认启用)将加载它。

如果您的应用程序被称为 home,那么您的模板目录将位于 home 目录中,而不是模板下的 home,这将使其可以作为 home/home.html 访问,因为它位于 settings.py 中模板目录的 home 子文件夹下

【讨论】:

    猜你喜欢
    • 2016-02-23
    • 1970-01-01
    • 2017-04-20
    • 2013-06-18
    • 2016-03-02
    • 2020-10-02
    • 1970-01-01
    • 2014-05-23
    • 2017-12-22
    相关资源
    最近更新 更多