what is {% block content %} and {% endblock content %} for in Django? - python

so I just started reading a book on Django (for beginners) and I came across the following code snipet:
<header>
Home | About
</header>
{% block content %}
{% endblock content %}
Could anyone possibly explain to me what is the use of {% block content %} and {% endblock content %}? Thank you very much in advance!

block is used for overriding specific parts of a template.
In your case, you have a block named content and this is supposed to be overridden by children that inherit from this template.
From the examples at The Django Docs
Template to be extended, named base.html
<head>
<link rel="stylesheet" href="style.css">
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
Overriding Child template
{% extends "base.html" %}
{% block title %}My amazing blog{% endblock %}
"My amazing site" will be overriden by the child and then display "My amazing blog"

That's where the power of the templates comes from in a sense.
You can create a hierarchy of templates so start with base.html which might be like you've got above;
<body>
{% block content %}
{% endblock content %}
</body>
Then you can create any other template, home.html for example, and do something like;
{% extends "base.html" %}
{% block content %}
<h1>Welcome</h1>
<p>This is the home page</p>
{% endblock content %}
Then you'd reference home.html in django and it'd include the markup from base.py with the content defined in home.html.
That's the basics, but if you put some templates together using blocks you'll pick it up.

For example, you have code excerpts from 2 files:
base.html:
<body bgcolor="cyan">
{% block content %}
{% endblock %}
</body>
home.html:
{% extends 'base.html' %}
{% block content %}
<h1>Hello World from Abhishek</h1>
{% endblock %}
here in home.html, the attributes of base.html will be extended but by using {% block content %} and {% endblock %} you will be able to override the code block of home.html upon the attributes of base.html
This is Jinja template for a dynamic website.

Related

Jinja extends, pushing content from child back to parent

I want to have a base template and child template that both extend blocks in both directions (between each other) is this possible?
base.html
<html lang="en">
<head>
</head>
<body class="">
<div>
{% block header_items %}{% endblock %}
</div>
<div style="min-height:90vh">
{% block content %}{% endblock %}
</div>
</div>
</body>
</html>
and then a child template
child.html
{% extends "schedule_layout.html" %}
{% block content %}
<h3>CONTENT</h3>
{% endblock %}
Is it possible to, from the child, push content into the "header_items" block in the base.html template. Bearing in mind I have multiple child templates all representing different pages on my site.
Any suggestions?
I can't also extend the main template in the child as then they both will be constant referencing each other.
I realise that some people may not even understand my question but i've managed to find out the answer I need.
The extends in the child template is able to handle mutiple blocks and it just didn't cross my mind. So to add content into the base.html in both blocks you just need them both in the child template also.
child.html
{% extends "schedule_layout.html" %}
{% block header_items %}add me to the base {% endblock %}
{% block content %}
<h3>CONTENT</h3>
{% endblock %}
Of course you do !!

base.html template only displays 1 block of code instead of multiple

I'm creating my base.html file and for some reason only one block is displaying in my base.html file at a time. I am trying to include a nav bar, a footer, and some content in base.html but it will only display one block at a time.
I have a feeling it has something to do with my view class because I am only including one file at a time, however I'm fairly new to starting a Django project and I don't know the common procedure to set up a base.html file.
base.html:
{% block nav_bar %}{% endblock %}
{% block content %}No Content to Show!!{% endblock %}
{% block footer %}No Footer Available!!{% endblock %}
views.py:
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'home.html'
Hoping to have all blocks show up on the page at once!
EDIT: home.html is my homepage content page.
If you are extending the 'home.html' from 'base.html'. Please check if you have the right structure as mentioned below and I recommend you to use spaces between the '}' or '{' and what will come after them.
base.html:
<!DOCTYPE html>
<body >
{% block nav_bar %}{% endblock %}
{% block content %}{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
home.html :
{% extends "base.html" %}
{% block nav_bar %} {% endblock %}
{% block content %} No Content to Show!! {% endblock %}
{% block footer %} No Footer Available!! {% endblock %}

How to make Django template recognize load tag upon inheriting from base template

I have the following base.html
{% load static from staticfiles %}
<html>
<title>COOL| {% block title %} Sometitle {% endblock %}</title>
<body>
<!--- BEGIN INSERT CONTENT FOR OTHER PAGE HERE-->
{% block 'body' %}
{% endblock %}
And I have somefile.html which are wrapped by the above.
{% extends 'base.html'%}
{% block title %} Contact {% endblock %}
{% block 'body' %}
<h1> CSV </h1>
{% endblock %}
The message I get is this:
Invalid block tag: 'static', expected 'endblock'
I expect somefile.html will inherit {% load static from staticfiles %} from base.html. But it doesn't. What's the right way to do it?
You should load tags in each template.

How to include block of code to every page except base.html

I need to create a small side block with form(it contains only one field and button) and I want it to be included to every page except base.html
I thought about making simple view function, but maybe there are better ways to do this?
I'm using Python and Django 1.6
In general, you shouldn't use base.html directly, but because you are and because it would be a huge hassle to change it in every other template, what you can do is, in the view function that returns base.html, you can add a boolean to the context and check the boolean to determine what template you are using.
Something like this:
def view_that_uses_base.html(request):
is_base = True
return render_to_response("base.html", {"is_base":is_base}, RequestContext(request,{}))
And then in the template:
{% block sidebar %}
{% if is_base%}
{% else %}
#Your code here
{% endif %}
{% endblock sidebar %}
You must use templates to do that.
In other words, try creating $DJANGO_ROOT/templates/main.html using the following code:
<html>
<head>
</head>
<body>
{% block one_field_and_a_button %}
<input />
<button>I am everywhere</button>
{% endblock %}
{% block my_custom_content %}
{% endblock %}
</body>
<html>
Then all other templates must extend that main.html template and insert their own data.
Imagine this is $DJANGO_ROOT/templates/login.html. It will only replace "my_custom_content" and will inherit all other blocks including "one_field_and_a_button"
{% extends 'templates/main.html' %}
{% block my_custom_content %}
Hello World! This is the login
{% endblock %}
Finally, if you want to have a base.html that does not have that part of the code containing one field and a button, you can do the following.
Imagine this is $DJANGO_ROOT/templates/base.html. It will replace both "one_field_and_a_button" and "my_custom_content". However, in this case, "one_field_and_a_button" will be replaced with blank space that will not show in your html code.
{% extends 'templates/main.html' %}
{% block one_field_and_a_button %} {% endblock %}
{% block my_custom_content %}
Hello World! This is my base.html template
{% endblock %}
Hope it works for you!
You can use block tag in base.html, i think you are searching foe something like this
base.html
{% block code %}
{% include 'sidebar.html' %}
{% endblock %}
index.html
{% extends base.html %}
{% block code %}
{% endblock %}
and every other templates
just extend base html
{% extends base.html %}

Jinja2 Inheritance with Blocks and Includes

I can't figure out how to modify blocks from included templates using Jinja2. Here's an example where I use three files.
base.html:
<html>{% include "content.html" %}</html>
content.html:
<h1>{% block title %}Title{% endblock title%}</h1>
<div>{% block content_body %}Content Body{% endblock content_body%}</div>
story.html
{% extends "base.html" %}
{% block title %}story.title{% endblock title %}
{% block content_body %}story.description{% endblock content_body %}
When rendering story.html, I'll get:
<html>
<h1>Title</h1>
<div>Content Body</div>
</html>
How would I render with the expected values?
base.html is not rendered because it's not invoked by any template. What you could do is a second level of extension:
base.html:
<html>{% block html %}{% endblock %}</html>
content.html:
{% extends "base.html" %}
{% block html %}
<h1>{% block title %}Title{% endblock title%}</h1>
<div>{% block content_body %}Content Body{% endblock content_body%}</div>
{% endblock %}
Still, that is probably overkill, you will likely find that a single base template is enough (i.e. combine base.html and content.html into a single template).

Categories