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
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
Is there a way to create a default entry, if a block isn't filled?
I have a template with a button that says create.
myTemplate.html:
create
Up until now, all pages using this template have used the test create, however now I would like to be able to edit that text from my page.
Usually I would add a block to the template to be overridden by the page:
myTemplate.html:
{% block createString %}{% endblock %}
myPage.html:
{% extends "myTemplate.html" %}
{% block createString %}next{% endblock %}
However, for this I would like to have something like a default, so that if I don't have any createString block in the myPage, a default string will be used.
Something like:
myTemplate.html:
<a href="#" class="btn">{% block createString %}{% default %}create
{% endblock %}</a>
In the myPage.html the button will say next while in the other files extending myTemplate.html the button will read create.
This was much simpler than I thought...
So in the template I just write the "default" code within the block.
myTemplate.html:
{% block createString %}create{% endblock %}
So in the pages I want the create text I simply ignore the createString block, while in the pages I want to change the text I override the template block like the following.
myPage.html:
{% extends "myTemplate.html" %}
{% block createString %}next{% endblock %}
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.
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 am using the django template system fine-though is there a way to use the same variable inheritance tag for more than one template without pulling in the data from the other template too.
<div id="content-container3">
{% block content-container2 %}{% endblock %}
</div>
So I want to use the above in say people.html template and test.html template.
for example:
{% extends "base.html "}
{% block content-container2 %}{% endblock %}
Though it cross refernces information from two templates in this case -does each variable inheriatnce tag have to be unique?
try to move this part of code to different file (content_container3.html) and use include tag... or I did not understand your question :)
I think you should try nesting blocks like the below example.
{% block first_section %}
{% block first_section_upper %}
{{block.super}}
{% endblock first_section_upper %}
<h1 class="display-5">Some content</h1>
<h5 style="color: white !important;" class="display-5"> *Your text here*</h5>
<p class="lead">The Algorithms that run our Universe</p>
{% block first_section_lower %}
{{block.super}}
{% endblock first_section_lower %}
{% endblock first_section %}
The h1 tag and p tags can be different for every new template. As far as i could understand, this might solve your problem.