Django templates - include and repeat the block contents - django

In my home.html page, I am trying to include a header.html file along with extending base.html. Following is my code
{% extends "base.html" %}
{% block body %}
{% include 'header.html' %}
# including the block navigation from header.html
<nav id='header-nav'>{% block nav %} {% endblock %}</nav>
# including the block image from header.html
<div id='header-img'>{% block image %} {% endblock %}</div>
# Reusing the same navigation in footer from header.html
<div id='footer-nav'>{% block nav %} {% endblock %}</div>
{% endblock %}
Home.html looks like the following
{% block image %}<h1>I am image</h1>{% endblock %}
{% block nav %}<h1>I am navigation</h1>{% endblock %}
However, it returns an error - ''block' tag with name 'nav' appears more than once'.
Why is that? Is there any solutions to this?
Regards

You included {% block nav %} twice in the same template. That's why it's throwing the error. Maybe you meant to do {% block footer %}?
{% extends "base.html" %}
{% block body %}
{% include 'header.html' %}
# including the block navigation from header.html
<nav id='header-nav'>{% block nav %} {% endblock %}</nav>
# including the block image from header.html
<div id='header-img'>{% block image %} {% endblock %}</div>
# Name this block something else i.e add a new block in header.html
# and this error should clear up.
<div id='footer-nav'>{% block footer %} {% endblock %}</div>
{% endblock %}

Related

Is it okay to have two base.html templates in django?

Is it okay to have multiple base.html templates in django? For instance, I would have one template that would extend from base_one.html and another template extending from base_two.html. For example, this is one of the templates:
{% extends "base_one.html" %}
{% block content %}
{% endblock content %}
and this is another template:
{% extends "base_two.html" %}
{% block content %}
{% endblock content %}
Well not only two you can keep how much you want just with different names and you have to extend on different templates but yeah you can easily keep parts of the base template and extend in one according to your needs.
I'm adding three files here 1-base.html 2-base-comments.html 3-post-template.html
Here is a little expansion of my answer
Suppose this file name is base.html
# base.html
<html>
<head>
<title>Foo</title>
</head>
<body>
<header>
{% block header %}
<h1>Lorem ipsum</h1>
{% endblock %}
</header>
{% block content %}{% comment %}A wrapper around content is needed{% endcomment %}
<div class="page-content">
{% block page_content %}{% comment %} Filled in by your page templates {% endcomment %}
{% endblock %}
</div>
{% endblock %}
<footer>
{% block footer %}
<em>♥ joar</em>
{% endblock footer %}
</footer>
</body>
</html>
here is another file base-comments.html which extends the previous file.
# base-comments.html
{% extends 'base.html' %}
{% block content %}
<div class="page-content">
{% block page_content %}{% comment %} Filled in by your page templates {% endcomment %}
{% endblock %}
{% block comments %}
<footer>
<h2>Comments</h2>
<script>loadCommentsEtc()</script>
</footer>
{% endblock %}
</div>
{% endblock %}
And here is the last file which extends the 2nd base file which already extends the 1st base file 3-post-template.html
# post-template.html
{% extends 'base-comments.html' %}
{% block page_content %}
<article>
<h1>{{ post.title }}</h1>
<div class="post-body">
{{ post.body }}
</div>
</article>
{% endblock %}
I hope this works and clears your doubts.
Thanks for the question.

Nesting Django block with {% include %}

I am trying to nest blocks with Django 3. I have sections of html that are sometimes reused on a page. I don't think I would have to resort to duplicating templates, but I can't get it to work;
I have this template
{% extends 'base.html' %}
{% include 'site_part.html' %}
{% block content %}
Some Content
<br>
And here I insert the child content
{% block part_of_site %}
{% endblock %}
{% endblock %}
And the site_part_html is like this;
{% block part_of_site %}
Okay, i am loaded!
{% endblock %}
In the base.html I have only this:
{% block content %}
{% endblock %}
I'd expect it to render the "Okay, i am loaded!" string in the resulting page, in the content block. However, it remains empty. I've looked, but most examples are far more advanced then what I need and I can't get those to work either.
If I remove the extends statement at that start and the block content lines, it does load the included html.
Basically, I have a site part that sometimes is need, and I'd like to included that based on some templating. Otherwise, I'd have duplicate that code for each of the pages that it occurs on.
You may call the content from the block 'part_of_site' in any child template using {{ block.super }} like this:
{% extends 'site_part.html' %}
{% block content %}
Some Content
<br>
{% block part_of_site %}
{{ block.super }}
{% endblock %}
{% endblock %}
You should use {% extends 'base.html' %} in the 'site_part.html' template. All children of 'site_part.html' will also be a descendant of base.html
{% extends 'base.html' %}
{% block part_of_site %}
Okay, i am loaded!
{% endblock %}
If you want to use {% include %} instead, change your code like this:
{% extends 'base.html' %}
{% block content %}
Some Content
<br>
{% include 'site_part.html' %}
{% endblock %}
You need to move your {% include 'site_part.html' %} into the {% block content %} block.
content.html
{% extends 'base.html' %}
{% block content %}
Some Content
<br>
And here I insert the child content
{% include 'site_part.html' %}
{% block part_of_site %}
{% endblock %}
{% endblock %}
Then, in your view you need to return your content template. I named it content.html here.
def your_view(request):
return render(request, "content.html")
You can put the include anywhere inside the content block, it doesn't have to be right before the part_of_site block.

Reuse same "block" of html in multiple django templates

Currently, I have two html template that extends from a base.html:
page1.html:
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
Code_block_1
{% endblock %}
page2.html:
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
Code_block_1
{% endblock %}
Both html share the same Code_block_1.
I was thinking about about creating another html called Code_block_1.html to consolidate this repeating piece of code. Then, insert Code_block_1.html into page1.html and pag2.html. Django only lets you extend once. How do I get around this problem?
Thanks.
Simply create another HTML file called code_block_1.html and then inside both page1.html and page2.html use include like this:
<!-- page1.html -->
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}
<!-- page2.html -->
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}

No content visible in django-bootstrap3

I am using Django 1.9 and demo template structure to display home.html.
I have included the following in the base.html file:
{% block bootstrap3_content %}
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
...
</div>
</nav>
<div class="container-fluid">
{% autoescape off %}{% bootstrap_messages %}{% endautoescape %}
{% block content %}(no content){% endblock %}
</div>
{% endblock %}
Navbar shows successfully but I do not get the Bootstrap message in the container below?
I am a new user to Django and this App as well. Please help!
Edit1:
bootstrap.html extends bootstrap3.html; base.html extends bootstrap.html; home.html extends base.html.
home.html
{% block title %}My_title{% endblock %}
{% block content %} This is <em>bootstrap3</em> for <strong>Django</strong>.
{% endblock %}
I get the title in the navbar but no message inside content block.
Make sure that you have
{% load bootstrap3 %}
at the top of your page since you are using this module. Make sure that you have
'bootstrap3',
in your installed apps.
From your code it looks like you are extending a template. If you are, be sure to have a
{% extends FooTemplate.html %}
in case you are loading boostrap3 in the parent template.

Djangocms template not showing up

I have a basic DjangoCMS up and running.
base.html contains:
{% block content %}{% endblock content %}
I also have feature.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
<div>
{% placeholder "feature2" %}
</div>
<div class="jumbotron"">
{% placeholder "feature" %}
</div>
<div>
{% placeholder "content" %}
</div>
{% endblock content %}
I added the "feature2" placeholder in the above, and it correctly displays for editing on the site.
I then added a new line to base.html:
{% block base_logo %}{% endblock base_logo %}
and created a new file, base_logo.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_logo %}
<div>
{% placeholder logo %}
</div>
{% endblock base_logo %}
I expected this to also appear on the site for editing, but it doesnt. I have added the base_logo.html to the CMS_TEMPLATES in settings.py and TEMPLATE_DIR is also pointing correctly.
What else do I need to do for Djangocms to pick up my new template?
Take a look at template inheritance.
You're trying to use two {% extends %} tags, which won't work. You should use the {% include %} tag for base_logo, because it seems you'd want to include this in many templates. This question provides more info.