Django-cms multiple inheritance does not work.
I have the following template structure:
base.html
{% block content %}
{% endblock content %}
page1.html
{% extends "base.html" %}
{% block test %}
{% endblock test %}
page2.html
{% extends "page1.html" %}
{% block content %}
<div>foo</div>
{% endblock content %}
{% block test %}
<div>foo</div>
{% endblock test %}
The problem is that the block test in page2 HTML is not rendered. Only the blocks from base.html are rendered. If I include block test in base.html is also gets rendered in page2
Does your base.html contain {% block test %}?
Your base template must contain the block you want to override. You can add additional blocks in child templates, but they must be contained within a base block.
See https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance
base.html is your top level template. This would normally be the thing that starts with an html doctype and ends with a </html> tag. Everything else has to have a place to go in there. All you have is a content block.
So where would the test block go? You've tried to put it in page1.html, but it still needs a place to go in the parent template.
Related
I created a file named base.html in the templates folder and written the code as follows:
{% block page_content %}{% endblock %}
Now, in another file named Hello_World.html I wrote the code as
{% extends "base.html" %}
{% block page_content %}
<h1>Hello, World!</h1>
{% endblock %}
can anyone please explain the working of this code clearly as I didn't come across the commands block and endblock in css.To the one who answers Thank you very much in Advance!
This is Django.
block is used for overriding specific parts of a template.
You could 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
I have a base.html template which contains basic html definitions .The other templates are defined below
// pages/dashboard.html
{% extends 'base.html' %}
{% block body %}
{% include 'components/nav.html' %}
{% include 'components/dashboard.html' %}
{% endblock %}
// components/dashboard.html
<div class="page-container">
<div class="main-page">
{% block dashboard %}
{% endblock %}
</div>
</div>
// mailer/new.html
{% extends 'pages/dashboard.html' %}
{% block dashboard %}
<h1>hello</h1>
{% endblock %}
The view renders mailer/new.html the problem is that the block containing <h1>hello</h1> is not working. Where am i going wrong with that ?
{% include %} doesn't work that way. See the note in the documentation for that tag:
Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.
I'm would like to use flask-admin and integrate it in my own layout which is based on flask-bootstrap. I don't care about the navbar that comes with flask-admin would just use the pure list view. I'm struggling to find an elegant solution, such that I don't have to write my own list.html. Structure is:
base.html:
{% extends "bootstrap/base.html" %}
{% block content %}
<div class="container">
{% block page_content %}
{% endblock %}
</div>
{% endblock %}
My normal other templates just extend this base.html and overwrite the page_content block. The idea is to have now my own \admin\master.html which should extend the page_content as well. Something like:
\admin\master.html
{% extends 'base.html' %}
{% block page_content %}
{% block body%} here most of the list.html from flask-admin should appear
{% endblock%}
{% endblock %}
It seems that flask-admin in list.html defines also a body block, which seems to overwrite the body block from the flask-bootstrap template. I had the impression that jijna2 templating is somehow hierarchical. e.g. blocks get filled from the direct extension.
I could easily create my own list.html, edit.html and create.html but would probably duplicate most of the code. Is there a more elegant solution?
I ended copying over everything and creating my own. I found required changes to most of those files to get things to display properly anyways.
I have a template block to override the class name in the inherited template. The resultant html from the block override is showing up the a malicious text.
Base.html:
<html>
<body>
{% block content %}
<h1 class="{% block heading_style %}Base{% endblock %}">Base Page Heading</h1>
{% endblock%}
</body>
</html>
Child.html:
{% extends "Base.html" %}
{% block content %}
{{block.super}}
{% block heading_style %}Login{% endblock %}
{% endblock %}
The block heading_style is the block I'm concerned about.
Resultant html:
As you can see the h1 class has been replaced with "Login" but it has also the started showing the "Login" as text after the h1 tag.
I'm using django 1.3.3 and eclipse with pydev. I've also checked the encoding of the html files and they and they are utf-8
You've put the definition of the heading_style block in the child within the content block. So it's being used for two things: as text content within content, and to fill the heading_style block in the parent.
Move heading_style outside of the content block definition, and it should be fine.
one endblock is missing
{% block content %}
{{block.super}}
{% block heading_style %}Login{% endblock %} {% endblock %}
I'm not sure overriding a block and nested sub-block along with {{block.super}} is good combination of doing things. May be you want to re-factor your templates for not relying on this.
Here is a ticket on django related to this (not sure this has been fixed or not) {{ block.super }} doesn't work with nested {% block %} statements
I am using the Django template system in my app, and in the child template I reference the {% extends "base.html" %} accordingly, though when redenring the template, it only comes up with the html tags and no css styling-thus not picking up the base.html template altogether.
Please advise on what I am doing wrong?. Could it be an issue with it not finding the location of my base.html template?
This is the child template code:
{% extends "base.html" %}
{% block content %}
{% for field in form %}
{{ form }}
{% endfor %}
{% endblock %}
This is the base.html template code (relevant parts):
<div id="content">
<h2>
Page heading - This is where the functionality goes...
</h2>
<FIELDSET><INPUT class=Test value="Test" type=submit></FIELDSET>
{% block primary %}{% endblock %}
</div>
Please let me know if you need any firther information to solve this issue.
-I will add the views.py this evening (London time)--
This all all sorted, many thanks for the help
Thanks
Like goliney mentioned...
You need to have a part in your base.html file that has
{% block content %}
{% endblock %}
Then you can override that in your other template that extends the base.html template.
Currently in your child template you could do a
{% block primary %}
whatever you want here and this would show up where you marked on the base.html template
{% endblock %}
Also not sure if you are just putting together a quick example or not. You will need to create the
<form action="" method="post">{% csrf_token %}
for loop here
</form>