extends not working in Django 1.9

我的未来我决定 提交于 2019-12-24 01:25:25

问题


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".


回答1:


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>


来源:https://stackoverflow.com/questions/37546991/extends-not-working-in-django-1-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!