假设我正在撰写应用程序“个人资料”;我有以下模板(在profile / presentation.html中):
<h1>{{user.first_name}}<h1>
<p> This user likes {{user.hobby}}</p>
附加到视图:
# profile/views.py
class DetailUserView(generic.DetailView):
model = User
template_name = 'members/profile_detail.html'
# profile/urls.py
urlpatterns = ( url(r'^(?P<pk>[0-9]+)$', views.DetailPersonView.as_view(), name='profile'))
# project/urls.py
urlpatterns = ( .., url(r'^profile/', include(profile.urls), ...)
现在我想在项目布局中“插入”这个模板:
<!-- project/templates/project/layout.html-->
<html>
<head>
<title>My Site</title>
</head>
<body>
<header>The title !!</header>
{%block body_block%}{%endblock%}
</body>
</html>
如果我想保持我的应用程序解耦,我不能从“layout.html”继承“presentation.hmtl”。
我怎么能对django说:当我们询问/ profile / 2时,取出“DetailUserView”视图的结果并将其插入“body_block”的布局中?
答案 0 :(得分:0)
您正在寻找Template Inheritance。这些文档有很多很好的信息。
<html>
<head>
<title>My Site</title>
</head>
<body>
<header>The title !!</header>
{%block body_block%}{%endblock%}
</body>
</html>
{% extends "project/layout.html" %}
{% block content %}
<h1>{{user.first_name}}<h1>
<p> This user likes {{user.hobby}}</p>
{% endblock %}