So I'm trying to add the footer and header on every page of my website. I made a base.html file which contains the general layout of the site.
In my about.html page, i did:
{% extends "public/base.html" %} <h1>Content goes here</h1>
I can see my header and footer, but how do I display the content. I want to type stuff in that about.html page. The content goes here isn't being displayed in the middle.
{% include %}: this will insert a template within the current one. Be aware that the included template will receive the request's context, and you can give it custom variables too.
Yes you can extend different or same templates.
extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables. Syntax: {% extends 'template_name.html' %}
Template. A template is a text file that defines the structure or layout of a file (such as an HTML page), it uses placeholders to represent actual content. A Django application created using startapp (like the skeleton of this example) will look for templates in a subdirectory named 'templates' of your applications.
You need to define a block in base.html and populate it in about.html.
base.html:
<header>...</header> {% block content %}{% endblock %} <footer>...</footer>
about.html
{% extends "public/base.html" %} {% block content %} <h1>Content goes here</h1> {% endblock %}
This is all fully explained in the tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With