Liondancer
Liondancer

Reputation: 16469

TemplateDoesNotExist. loading static files and templates Django project structure

I am a bit confused on how to yield templates from other apps

Here is a picture of my project structure:

enter image description here

I am getting this error:

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'),
)

project/home/urls.py:

from django.conf.urls import patterns, url

from home.views import HomeView

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

project/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")),

)

project/templates/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 %}

Upvotes: 1

Views: 1196

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

Django stops at the first template that it finds matching the requested filename, and it will start at the templates directory in the app folder.

If it doesn't find it here, it will then go up the chain till it has searched all locations in the TEMPLATE_DIRS setting, and only then will it print an error that it cannot find a template. The way it searches for templates is defined by the TEMPLATE_LOADERS setting.

In your project layout, the template you want is located in the template directory of the home app, and inside this directory, under the home directory. Thus the template path should be home/home.html in your view:

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

Upvotes: 2

user324747
user324747

Reputation: 273

If your templates directory is in your project's root you have to add it to setitngs.py

If it's the subdirectory of an app (that is in INSTALLED_APPS in settings.py) then the app template loader (enabled by default) will load it.

If your app is called home then you would have the templates dir inside the home dir, not home under templates which would make it accessible as home/home.html since it's under the home subfolder of the templates dir in settings.py

Upvotes: 1

Related Questions