You are advised to check corresponding YouTube video at the end of this article.
In templates directory we will create parts directory with header.html and footer.html files.
Then, we need to include header and footer in base template, .base.html. Block section for listing will remain in base template, because custom pages will perhaps use that block.
{% include 'parts/header.html ' %}
{% block listing %}
{% endblock %}
{% include 'parts/footer.html ' %}
index.html will remain uchanged:
{% extends 'base.html' %}
<h1>All Posts: </h1>
{% block listing %}
{% for post in all_posts %}
<ul>
<li>{{ post.id }} - {{ post.title }} - {{ post.text }}</li>
</ul>
{% endfor %}
{% endblock %}
This will be in header.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul>
<li><a href="{% url 'index' %}">Home</a></li>
<li><a href="">New Post</a></li>
<li><a href="">Admin</a></li>
</ul>
<hr>
And footer.html will be minimal:
</body>
</html>
If you have a lot of code in header.html, consider having navigation in dedicated section:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
{% block navigation %}
<ul>
<li><a href="{% url 'index' %}">Home</a></li>
<li><a href="">New Post</a></li>
<li><a href="">Admin</a></li>
</ul>
{% endblock %}
<hr>
No comments:
Post a Comment