Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Footer and header on each page with {% extends }

Tags:

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.

like image 812
encrypt Avatar asked Jul 16 '15 14:07

encrypt


People also ask

What does {% include %} means in Django?

{% 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.

Do Django templates have multiple extends?

Yes you can extend different or same templates.

What does Extends mean in Django?

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' %}

What is Template explain with suitable example in Django?

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.


1 Answers

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.

like image 154
Daniel Roseman Avatar answered Sep 20 '22 14:09

Daniel Roseman