扩展在 Django 1.9 中不起作用
extends not working in Django 1.9
当我将 {% extends "X.html" %}
添加到我的子模板(父模板为 "base.html")时,仅加载父模板。当我拿走它时,子模板被加载。我有另一个应用程序,其中我有一个看似相同的继承结构,所以我很难过。这里是 "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>
这里是"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 %}
我肯定是在 views.py 中调用子模板,所以这不是问题所在:
def grow(request):
...
return render(request, 'app/grow.html')
这是我的项目 urls:
来自 django.conf.urls 导入包括,url
来自 django.contrib 导入管理员
从应用导入视图
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^grow/', include('app.urls')),
url(r'^$', views.home, name="home"),
]
和应用 urls:
from django.conf.urls import url
from app import views
urlpatterns = [
url(r'^$', views.grow, name='grow'),
]
当我去 url /grow/ 时,我期待看到 "grow.html" 但我看到的是 "base.html".
您的基本模板不包含 content
块。所以它是继承的,它只是无处可去。
只需添加内容块。
</head>
{% block content %}
<body>
<h1>Welcome to my app</h1>
</body>
{% endblock content %}
</html>
当我将 {% extends "X.html" %}
添加到我的子模板(父模板为 "base.html")时,仅加载父模板。当我拿走它时,子模板被加载。我有另一个应用程序,其中我有一个看似相同的继承结构,所以我很难过。这里是 "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>
这里是"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 %}
我肯定是在 views.py 中调用子模板,所以这不是问题所在:
def grow(request):
...
return render(request, 'app/grow.html')
这是我的项目 urls:
来自 django.conf.urls 导入包括,url 来自 django.contrib 导入管理员 从应用导入视图
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^grow/', include('app.urls')),
url(r'^$', views.home, name="home"),
]
和应用 urls:
from django.conf.urls import url
from app import views
urlpatterns = [
url(r'^$', views.grow, name='grow'),
]
当我去 url /grow/ 时,我期待看到 "grow.html" 但我看到的是 "base.html".
您的基本模板不包含 content
块。所以它是继承的,它只是无处可去。
只需添加内容块。
</head>
{% block content %}
<body>
<h1>Welcome to my app</h1>
</body>
{% endblock content %}
</html>