Django check block content is none or not - html

Can I code something like this? :
// base.html
{% block content %}
{% if block content is None %}
<p> default content </p>
{% endif %}
{% endblock content %}
// child.html
{% block content %}
<p> child content </p>
{% endblock content %}
I know I just need to code something like this:
// base.html
{% block content %}
{% endblock content %}
// child.html
{% block content %}
{% if child.content %}
<p> child content </p>
{% else %}
<p> default content </p>
{% endif %}
{% endblock content %}
But I have so many child.html inherit from base.html.. So I just want to change the parent(base.html). Is it possible?

{% extends 'base.html' %}
{% block content %}
{% endblock content %}
{% include 'child.html' %}
{% block content %}
{% if child.content %}
<p> child content </p>
{% else %}
<p> default content </p>
{% endif %}
{% endblock content %}
i belive you looking something like can we extends something multiples times this is how we do it we can use extends only once then we need to use include and give path i hope it helps you if not let me know i will improve the answer

Related

html block content printed as {% block content %} {% endblock %} string

test.html
{% extends "main.html" %}
{% block content %}
<p>block test</p>
{% endblock %}
I add to main.html
{% block content %}{% endblock %}
main.html and test.html are in the same directory.
but in the main page literally {% block content %} {% endblock %} are printed like this ↓.
I don't know why. I've tried {% extends "./main.html" %} but also doesn't work.

why i don't see all parts in html using django

I wrote the following code:
{% extends "base.html" %}
{% block content %}
{% if all_site %}
<ul>
<h3>Here all my site:</h3>
{% for site in all_site %}
<li> {{ site.name }}</li>
{% endfor %}
</ul>
{% else %}
<h3> You don't have any Site.</h3>
{% endif %}
{% endblock content %}
when, I run. I not see "Here all my site", but I only see the one contained in the for.
I tried to modify it, for now a solution that I have found working is this:
{% extends "base.html" %}
{% block content %}
{% if all_site %}
<h3>mmmmmm</h3>
<ul>
<h3>Here all my site:</h3>
{% for site in all_site %}
<li> {{ site.name }}</li>
{% endfor %}
</ul>
{% else %}
<h3> You don't have any Site.</h3>
{% endif %}
{% endblock content %}
in this case if I do run, i see:
here all my site:
site 1
site 2

Is there a way to use block of code from child file to another child file using Django?

base.html:
<html>
<body>
{% block content %}
</body>
</html>
child1.html:
{% extends "base.html" %}
{% block content %}
{% block upperDisplay %}
{% endblock %}
child2.html:
{% extends "base.html" %}
{% block content %}
..code for child2 to base
{% end block%}
child11.html (childs for child1):
{% extends "child1.html" %}
{% block upperDisplay %}
..code for child11 to child1
{ %end block% }
Child12:
{% extends "child1.html" %}
{% block upperDisplay %}
<div>
<table>
.... some code ..
</table>
{% block legend %}
<div>
.. some code ..
</div>
{% end block %}
</div>
{% end block %}
Here I wish to use {% block legend %} also in child1.html, child11.html.
Can you tell me how to do this Django?
Thanks in advance.
Separate that in another file and then include it.
legend_template.html
{% block legend %}
<div>
..some code..
</div>
{% end block %}
Now in other children:
{% include "legend_template.html" %}

Including variables from a child template

I have several templates:
parent.jinja2
{# Header #}
{% block content %}
{% block title_header %}
<h1>{{ the_title }}</h1>
{% endblock %}
{% block child_content %}
{% endblock %}
{% include 'extra.jinja2' %}
{% endblock %}
{# Footer #}
extra.jinja2
{% block extra %}
<p>The title was {{ the_title }}.</p>
{% endblock %}
child.jinja2
{% extends 'parent.jinja2' %}
{% set the_title = "Title of doom" %}
{% block child_content %}
<p>Some stuff.</p>
{% endblock %}
When I render child.jinja2, the value of the the_title in extra.jinja2 is empty. How can I access the value of the_title that is defined in child.jinja2?
The problem seems to go away if I remove the title_header block, so looks to be something to do with first reading the_title inside that block.

Twig include render two time block

I've some trouble with Twig. I don't understand why one of my block is rendering two time in my page.
A-propos.html.twig
{% extends "::layout-v2.html.twig" %}
{% set contexte = "a-propos" %}
{% block content %}
//some stuff
{% block rightbar %}
{{"Je suis dans le block de la vue rightbar"}}
{{ parent() }}
{% endblock %}
//some stuff
{% endblock %}
layout-v2.html.twig
{% include '::header/header-v2.html.twig' %}
<body class="">
{% block topBar %}
{% endblock %}
{% if app.user %}
{% include '::layout-user-v2.html.twig' with {'view_content': block('content')} %}
{% else %}
{% include '::layout-public-v2.html.twig' with {'view_content': block('content')} %}
{% endif %}
{% block rightbar %}
<div class="col-sm-3">
{% if contexte is defined %}
{% include 'BtpGeneralBundle:Sidebars:sidebar_contexte.html.twig' %}
{% else %}
{% include 'BtpGeneralBundle:Sidebars:sidebar_default.html.twig' %}
{% endif %}
</div>
{% endblock %}
{% include '::footer/footer-js-v2.html.twig' %}
</body>
I don't understand why the view sidebar-context.html.twig is rendering first time on the right place and another time just before the include footer-js...
Thank
You could split {% block content %} in A-propos.html.twig on two parts, e.g. contetn1 before rightbar and contetn2 after.