创建基模板
blog\templates\base.html
<!-- base.html -->
Generic welcome to your web page [Logion - Help -FAQ]
<h1>Blog Central</h1>
{% block content %}
{% endblock %}
© 2019 your company [About - Contact]
</body>
</html>
创建子模板
blog\templates\index.html
<!-- index.html -->
{% extends "base.html" %}
{% block content %}
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.timestamp }}</p>
<p>{{ post.body }}</p>
{% endfor %}
{% endblock %}
使用子模板
blog\urls.py
urlpatterns = [
url(r'^$', index),
...
]
blog\views.py 新增index处理
def index(request):
posts = BlogPost.objects.all()[:5]
return render(request, 'index.html',{'posts':posts, 'form':BlogPostForm})