简体   繁体   中英

extends not working in Django 1.9

When I add {% extends "X.html" %} to my child templates (the parent being "base.html"), only the parent template is loaded. When I take it away, the child template is loaded. I have another app where I have a seemingly identical inheritance structure, so I'm stumped. Here is "base.html":

<!DOCTYPE html>
{% load staticfiles %} 
<html>
  <head>
    {% block js %}
    <script src="{{ STATIC_URL }}js/jquery.1.12.4.min.js"></script>
    <script src="{{ STATIC_URL }}js/p5.js"
    {% endblock %}
    <title>myapp</title>
  </head>
  <body>
    <h1>Welcome to my app</h1>
  </body>
</html>

Here is "grow.html"

<!DOCTYPE html>
{% extends "app/base.html" %}
{% block js %}
  <script src="{{ STATIC_URL }}js/grow.js"></script>
{% endblock %}
{% block content %}
<body>
  <div id="message" style="visibility: hidden;"></div>
  <div id="tree"></div>
  <a href="/register/">register</a>
<form method="POST">
  {% csrf_token %}
  <input type="text" id="txt" />
  <input type="submit" id="grow" value="grow" style="color: grey;"/>
</form>
</body>
{% endblock %}

I am definitely calling the child template in my views.py, so that's not the problem:

def grow(request):
    ...
    return render(request, 'app/grow.html')

Here are my project urls:

from django.conf.urls import include, url from django.contrib import admin from app import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^grow/', include('app.urls')),
    url(r'^$', views.home, name="home"),    
    ]

and app urls:

from django.conf.urls import url
from app import views

urlpatterns = [
    url(r'^$', views.grow, name='grow'),

    ]

When I go to the url /grow/ I am expecting to see "grow.html" but I am seeing "base.html".

Your base template doesn't contain a content block. so it is inheriting, it just has nowhere to stick the block.

Simply add the content block.

  </head>
{% block content %}
  <body>
    <h1>Welcome to my app</h1>
  </body>
{% endblock content %}
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM