问题描述
当我向我的子模板(父为base.html)添加 {%extendsX.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>欢迎使用我的应用< / h1>
< / body>
< / html>
这是grow.html
<!DOCTYPE html>
{%extendsapp / base.html%}
{%block js%}
< script src ={{STATIC_URL}} js / grow.js>< ; /脚本>
{%endblock%}
{%block content%}
< body>
< div id =messagestyle =visibility:hidden;>< / div>
< div id =tree>< / div>
< a href =/ register /> register< / a>
< form method =POST>
{%csrf_token%}
< input type =textid =txt/>
< input type =submitid =growvalue =growstyle =color:gray/>
< / form>
< / body>
{%endblock%}
我绝对在我的views.py中调用子模板,所以这不是问题:
def grow(request):
...
return render (请求'app / grow.html')
这是我的项目网址:
from django.conf.urls import include,url
from django.contrib import admin
from app import views
urlpatterns = [
pre>
url(r'^ admin /',admin.site.urls),
url(r'^ grow /',include ('app.urls')),
url(r'^ $',views.home,name =home),
]
和应用程序网址:
from django.conf.urls从应用程序导入视图导入url
urlpatterns = [
url(r'^ $',views.grow,name ='grow'),
]
当我去url / grow /我期待看到grow.html 但我看到base.h tml
解决方案您的基本模板不包含
内容
块。所以它是继承,它只是没有地方坚持块。
只需添加内容块。
< / head>
{%block content%}
< body>
< h1>欢迎使用我的应用< / h1>
< / body>
{%endblock content%}
< / html>
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, urlfrom django.contrib import adminfrom 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>
这篇关于扩展在Django 1.9中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!