I have this in my base.html template file:
<body class="{% block body_class %}{% endblock %}">
Then this in my view template file:
{% block body_class %}my_class{%%}
The outputted HTML looks like this:
<body class="my_class">my_class ...
Am I missing something?
UPDATE
base.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body class="{% block body_class %}{% endblock %}">
{% block header %}{% endblock %}
{% block content %}{% endblock %}
{% block footer %}
{% endblock %}
</body>
</html>
app/base.html
{% extends 'base.html' %}
app/view.html
{% extends 'app/base.html' %}
{% block content %}
{% block body_class %}login{% endblock %}
{% endblock %}
SOLVED
Figured out while typing the update. The problem was in using the {% block body_class %} inside {% block content %}
Related
I am using django-leaflet to display on my website and It works fine and displays the map on browser when I include the leaflet_map in the base template but when I use leaflet_map on a template that extends that base template then map doesn't appear on the browser.
This is the code of extended template from base.html and it doesn't show the map on browser.
{% extends 'base.html' %}
{% load leaflet_tags %}
{% block leaflet %}{% leaflet_js %}{% leaflet_css %}{% endblock %}
{% block content %}
{% leaflet_map 'gis' %}
{% endblock content%}
These are the snippets from the base template.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} {% endblock %}</title>
<!-- Leaflet Info goes here -->
{% block leaflet %}{% endblock %}
</head>
<body>
<div class="content">
{% block content %}{% endblock %}
</div>
I'm creating a Django project, where I want to use multiple levels of inheritance in my templates. E.g I want to do something like this:
project_base.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet">
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Then in app_base.html I extend this.
{% extends "project/project_base.html" %}
{% block title %}Generic Title{% endblock %}
{% block content %}
<img src="/dir/sub_dir/image.jpg">
{% block app_content %}
{% endblock %}
{% endblock %}
Finally I have my actual template
{% extends app_base.html %}
{% block title %}Specific Title{% endblock %}
{% block app_content %}
{% for obj in objects %}
{{ obj.name }}
{% endfor %}
{% endblock %}
The problem that I have is that when I go to load that page I see a single heading from an entirely unrelated template, and not my list of hyperlinks. What's the correct/best way to have multiple levels of inheritance for my template files?
I have 2 templates and I want to override the variable body_class from main template. This is the code I have so far.
Main Template:
{# main.html.twig #}
<!DOCTYPE html>
<html>
<head>
{% block head %}
{% block head_meta %}
// ...
{% endblock %}
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}
// ...
{% endblock %}
{% block icon %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
{% endblock %}
{% endblock %}
</head>
{% set body_class = '' %}
<body class="main {{ body_class }}">
{% block navi %}{% endblock %}
{% block error %}{% endblock %}
{% block body %}{% endblock %}
{% block footer %}{% endblock %}
{% block javascripts %}
// ...
{% endblock %}
</body>
</html>
Second template:
{# dashboard.html.twig #}
{% set body_class = 'dashboard' %}
{% extends 'main.html.twig' %}
{% block head %}
{{ parent() }}
{% endblock %}
{% block head_meta %}
{{ parent() }}
{% endblock %}
{% block title %}Dashboard!{% endblock %}
{% block body %}
I'm dashboard!
{% endblock %}
I tried placing {% set body_class = 'dashboard' %} before extend statement and after, but that didn't work. What am I doing wrong?
Thank you.
You should not set body_class in your main.html.twig file. dashboard.html.twig is all good, just change main.html.twig:
Remove {% set body_class = '' %} line and change <body class="main {{ body_class }}"> to <body class="main {{ body_class|default('') }}">.
That way you have your body_class variable value '' as a default value when it's not defined and 'dashboard' when you set it from your dashboard.html.twig file.
If you set a variable in your base file, in this case main.html.twig, then you can't overwrite it from inheriting templates.
Variables from child themes are accessible by their parents but get overriden as soon as you redeclare them in the parent.
So you can simply declare the variable in the parent only if is not defined:
{# main.html.twig #}
...
{% set body_class = body_class is defined ? body_class : '' %}
<body class="main {{ body_class }}">
...
{# dashboard.html.twig #}
{% extends 'main.html.twig' %}
{% set body_class = 'dashboard' %}
In my Django base.html template I have a title block:
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
In a page template I use the text from its <h1> page title in that block, so it also appears in the <title> tag, e.g.:
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %}Hello world!{% endblock %}</h1>
{% endblock %}
That all works fine. But if I want to use HTML tags within the page's <h1> like this...
{% extends 'base.html' %}
{% block content %}
{% block title %}<b>Hello</b> world!{% endblock %}
{% endblock %}
...those tags will also appear in the <title>, which isn't allowed.
Is there a way around this other than having two versions of the title: one within <h1>, and one HTML-free version within a {% block title %}? I don't think there's a way to strip HTML from a block?
You could use cycle like so:
{% extends 'base.html' %}
{% block content %}
<h1>
{% block title %}
{% cycle '' '<b>' %}Hello world!{% cycle '' '</b>' %}
{% endblock %}
</h1>
{% endblock %}
Hope this helps
I have two files, base.html and homepage.html. Homepage extends base, and adds to block extra. Base should render block extra in a span within the body, but doesn't.
base.html:
{% load sekizai_tags %}
<!doctype html>
<html>
<head>
<title>Title</title>
</head>
<body>
<span style="color: red;">{% render_block "extra" %}</span>
{% block 'content' %}
{% endblock %}
</body>
</html>
homepage.html:
{% extends 'base.html' %}
{% load sekizai_tags %}
{% block 'content' %}
<p>that's some sweet content ya got there lad</p>
{% addtoblock "extra" %}wow{% endaddtoblock %}
{% endblock %}
And the output:
What really simple thing am I missing?
Aha, I was missing a context preprocessor from my templates.
'sekizai.context_processors.sekizai',