Blocks in included files not being filled by extended templates - python

I have a template that looks like this:
{% include "base/top.html" with context %}
{% include "base/nav.html" with context %}
<div id="content">
Stuff
{% block content %}{% endblock %}
</div>
{% include "base/bottom.html" with context %}
base/nav.html and base/bottom.html contain static content, but base/top.html contains a {% block title %}. So when I have a second template as that attempts to inherit from the first file like so:
{% extends firstfile.html %}
{% block title %}Imarealpage!{% endblock %}
{% block content %}Lorem ipsum dorem smitshm{% endblock %}
The {% block title %} section isn't rendered. How do ensure that it, and any other blocks in included files and defined in extended templates are rendered as they should be?

You're misunderstanding how {% include %} works. The {% include %} tag is not a pre-processor; it doesn't plop the included template's code directly into the including template before rendering. Instead, {% include %} fires off a new independent template render of the included template (just like as if you had rendered the included template directly from your own code), and then includes the rendered results into the rendering of the included template.
The implication of this is that included templates have a totally separate inheritance hierarchy from their including template. You can, for instance, have a base component.html template with some blocks in it, and then have e.g. foo-component.html which starts with {% extends "component.html" %} and fills in some blocks from component.html. And then you can have a layout.html that does {% include "foo-component.html" %}, and that will render foo-component.html, complete with its inheritance of component.html, and place the result into that spot in layout.html. But there is zero relationship between any blocks in layout.html and any blocks in component.html -- they are separate renders with separate block structures and inheritance hierarchies.

Related

How can I apply base.html file for extending to every template in django

Is there a way to extend base.html file in python django by default, I want it because I am overwriting {% extends %} tag in every html file
No but what you can do however is created a new HTML file called for example, header-tags.html with something like that :
{% extends "base.html" %}
{% load static %}
.... more tags
And then include this snippet wherever you want with {% include 'header-tags.html' %}

Django: Is it possible to call a template from inside another template? [duplicate]

I have a very basic template (basic_template.html), and want to fill in the with data formatted using another partial template. The basic_template.html might contain several things formatted using the partial template.
How should I structure the code in views.py?
The reason I am doing this is that later on the will be filled using Ajax. Am I doing this right?
You can do:
<div class="basic">
{% include "main/includes/subtemplate.html" %}
</div>
where subtemplate.html is another Django template. In this subtemplate.html you can put the HTML that would be obtained with Ajax.
You can also include the template multiple times:
<div class="basic">
{% for item in items %}
{% include "main/includes/subtemplate.html" %}
{% endfor %}
</div>
You can do this using a block. Blocks are a Django Template tag which will override sections of a template you extend. I've included an example below.
basic_template.html
<body>
{% block 'body' %}
{% endblock %}
</body>
template you want to include: (i.e. example.html)
{% extends 'basic_template.html' %}
{% block 'body' %}
/* HTML goes here */
{% endblock %}
views.py:
return render_to_response(template='example.html', context, context_instance)
Doing this will load basic_template.html, but replace everything inside of {% block 'body' %} {% endblock %} in basic_template.html with whatever is contained within {% block 'body' %} {% endblock %}.
You can read more about blocks and template inheritance in the Django Docs
There are mainly 2 ways (2 easy ones)
1:
In base html put
{% include "myapp/sub.html" %}
And just write html code inside your sub.html file
2:
https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance
I just wanted to add differences of extend and include.
Both template and include can use models inserted in current app.
Template is for global usage by your any app. Include is for use in certain apps.
For ex: you want to insert Image Slider to your homepage and about page but nowhere else. You can create Slider app with its own model for convenience and import its model and include in that pages.
If you used template for this example, you would create 2 templates one with slider and everything else other template have.

Creating template from different block in django?

I am trying to create a single page with different content from different template in django so i can print it. Its kind of summary of different page
base.html - mainContent block is rendered inside this template
main.html - Need mainContent block from here
graph.html - Need mainContent block from here
charts.html - Need mainContent block from here
summary.html - Need content from main, graph, charts all together here (REQUIRE)
I have a base template which is extended on every page like this (It has navbar, footer and sidebar)
{% extends "base.html" %}
There is a block inside base template where graph, main, chart content is displayed. What i am trying to accomplish is to get the mainContent from those page and add it to new template called summary.html
Since every page is extending from base i am not sure how to do that? I tried using include but it will also include base for every page.
EDIT: I know i can separate the mainContent into its own separate files but i have lot of templates and was looking for any other solutions.
You could separate the content.
I guess you have something like this:
<!-- graph.html -->
{% extends "base.html" %}
{% block 'mainContent'%}
{# Your graphs html here #}
{% endblock %}
But you could put the graph html in a separate, let's say graph_template.html template and include it:
<!-- graph.html -->
{% extends "base.html" %}
{% block 'mainContent'%}
{% include 'graph_template.html' %}
{% endblock %}
And then in summary.html you can include graph_template.html.

How to remove breadcrumbs in Django admin

How can I use nav-global for navigation and hide the breadcrumbs in the django admin app. I've found ways to do this but they seem hackish and problematic. I'm looking for a clean reliable solution.
Naive Approach that doesn't work:
customize admin/base_site.html
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'Django administration' %}</h1>
{% endblock %}
{% block nav-global %}Some links will go here...{% endblock %}
{% block breadcrumbs %}{% endblock %}
this doesn't work because the templates in django admin like change_list.html that extend base_site.html will define content for breadcrumbs which will override whatever I have set in base_site.html.
solution 1: CSS
Add the following css to admin using the extrastyle block. This works, but the breadcrumbs still get generated and appear in the HTML source. This feels hacky.
.breadcrumbs {
display: none;
}
Solution 2: override base.html
Override base.html and remove the breadcrumbs block. Children will try to define it but it never exists so it never gets rendered. This also seems like a hack. It's also not a good idea to override base.html as each Django release can make many changes to base.html and the admin app could break between releases.
Extend all templates that you want to render an empty breadcrumbs block. Your first attempt (you say doesn't work) will work, just extend the other templates as well. Like change_list.html:
{% extends "admin/change_list.html" %}
{% block breadcrumbs %}
{% endblock %}
change_form.html:
{% extends "admin/change_form.html" %}
{% block breadcrumbs %}
{% endblock %}
The same for:
500.html
app_index.html
change_password.html
delete_confirmation.html
delete_selected_confirmation.html
invalid_setup.html
object_history.html
login.html and index.html have an empty breadcrumb block.
Downside is that the templates need to be applied per app. This means duplicated templates. If you want to do project wide with single templates you end up with other hacks: copying the complete original templates OR let the templates extend /path/to/site-packages/django/contrib/admin/templates/template_name.html.
Use CSS. You can also hide app_label which is 2nd child.
.breadcrumbs>ul li:nth-child(2),
#grp-breadcrumbs>ul li:nth-child(2) {
display:none;
}
To avoid the recursion problem:
Install django-apptemplates
pip install django-apptemplates
Add the template loader to your settings.py. If you are using Django >= 1.8, add it in the TEMPLATES setting (more details in the doc).
You can now override a template by adding app_name: in the extends tag:
{% extends "admin:admin/change_form.html" %}
{% block breadcrumbs %}
{% endblock %}

webapp2, Jinja2: how to cut large html file into multiple html files

When I blog, I like to separate each blog-post into its own .html file (is that ok?)
This prevents the file getting too big, and makes it easy to go back and edit a previously written blog post if need be.
Occasionally the blog post will contain css/js/ajax/template variables.
But on my website, I like all the blog posts on one page (so I can scroll through them all, instead of going to a separate page for each post)
Here is an html file that contains two blog posts:
{% extends "base.html" %}
{% block blog_posts %}
<!-- links/targest for the side menu to jump to a post -->
<li>Post2 - April 2012</li>
<li>Post1 - Feb 2012</li>
{% endblock %}
{% block content %}
<div id="post1">
spam1 blah blah
</div>
<div id="post2">
spam2
</div>
{% endblock %}
and in base.html I have something like:
<div id="content-container">
<div id="section-navigation">
<ul>
{% block blog_posts %}
{% endblock %}
</ul>
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</div>
What is the best way for me to split these blog posts out into separate files using webapp2 and jinja2?
e.g. blog1.html might look like:
{% block blog_posts %}
<!-- links/targest for the side menu to jump to a post -->
<li>Post1 - Feb 2012</li>
{% endblock %}
{% block content %}
<div id="post1">
spam1 blah blah
</div>
{% endblock %}
(And I would want the links and the blogposts to be displayed in the right order on the website)
I could think of a way of doing it where post2 extends post1.html, post3 extends post2.html etc, but I would prefer to fan out more
"Henry and Kafura introduced Software Structure Metrics Based on Information Flow in 1981[2] which measures complexity as a function of fan in and fan out."
Thanks
#robert king, your design has data embedded directly in the template. Templates should only contain the blueprint to a view, and they should be rendered with new data generated from your main code every time. I simulate this process here (Edited to illustrate the use of a loop to extract post titles, and the display of a single post.):
import jinja2
# NOTE: in this template there is no data relating to specific posts.
# There are only references to data structures passed in from your main code
page_template = jinja2.Template('''
<!-- this is a navigation block that should probably be in base.html -->
{% block blog_posts %}
<!-- links/targets for the side menu to jump to a post -->
{% for post in posts %}
<li><a href="{{ post.url }}">{{ post.title }}
- {{ post.date }}</a></li>
{% endfor %}
{% endblock %}
<!-- this is a content block that should probably be in page.html -->
{% block content %}
<div id="post">
<h1>{{ current.title }}</h1>
<h2>{{ current.date }}</h2>
<p>{{ current.content }}</p>
</div>
{% endblock %}
''')
# NOTE your main code would create a data structure such as this
# list of dictionaries ready to pass in to your template
list_of_posts = [
{ 'url' : '#post1',
'title' : 'My first post',
'date' : 'Feb 2012',
'content' : 'My first post is about Hello World.'},
{ 'url' : '#post2',
'title' : 'My second post',
'date' : 'Apr 2012',
'content' : 'My second post is about Foo Bar.'}
]
# Pass in a full list of posts and a variable containing the last
# post in the list, assumed to be the most recent.
print page_template.render(posts = list_of_posts,
current = list_of_posts[-1])
Hope this helps.
EDIT See also my answer to a question on "Site fragments - composite views"
I just found another option in the jinja2 tutorial. I think it makes more sense for my handler to pass my template a list of filenames of blog posts, and then to include the blog posts.
include - returns the rendered contents of that file into the current namespace:
{% include 'header.html' %}
<div ...
{% include 'footer.html' %}
Included templates have access to the variables of the active context by default. For more details about context behavior of imports and includes see Import Context Behavior.
From Jinja 2.2 onwards you can mark an include with ignore missing in which case Jinja will ignore the statement if the template to be ignored does not exist. When combined with with or without context it has to be placed before the context visibility statement. Here some valid examples:
{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with context %}
{% include "sidebar.html" ignore missing without context %}
New in version 2.2.
You can also provide a list of templates that are checked for existence before inclusion. The first template that exists will be included. If ignore missing is given, it will fall back to rendering nothing if none of the templates exist, otherwise it will raise an exception. Example:
{% include ['page_detailed.html', 'page.html'] %}
{% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}
When I read the raw html file (file.read()) and passed the data to my template, it escaped all the html.
instead of {{data}} i had to use {{data|safe}} which allowed raw html.
something like:
class HomeHandler(BaseHandler):
def get(self):
file_names = sorted(os.listdir('blog_posts'))
html = [open('blog_posts/%s' % fn).read() for fn in file_names]
templates = {'html': enumerate(html)}
self.render_template('home.html', **templates)
{% block content %}
{% for num,data in html %}
<div id="post{{num}}">
{{data|safe}}
</div>
<br />
<img src="http://www.sadmuffin.net/screamcute/graphics/graphics-page-divider/page-divider-007.gif" border=0>
<br />
{% endfor %}
{% endblock %}
(make sure the directory isn't a static directory)

Categories

Resources