Nest {% block %} in {% for %} Django template inheritance - python

I am developing a dictionary application using Django. One of the main themes of the app is displaying feeds of definitions.
Specifically, I have:
an "index" feed where new definitions are listed,
a "headword" feed where definitions of a particular headword are listed,
and a "tag" feed where definitions tagged with a particular tag are listed.
index.html:
{% block body %}
<h1>Definitions</h1>
<ul>
{% for definition in definitions %}
// definition HTML
{% endfor %}
</ul>
{% endblock %}
headword.html:
{% block body %}
<h1>{{ headword }}</h1>
<ul>
{% for definition in headword.definitions_headword.all %}
// definition HTML
{% endfor %}
</ul>
tag.html:
{% block body %}
<h1>{{ tag }}</h1>
<ul>
{% for definition in tag.definitions_tag.all %}
// definition HTML
{% endfor %}
</ul>
Clearly, I need a base feed.html template that these three files can share. For the h1 tag no problem. However, I am having problems with the for loop iterable variables.
I tried nesting a {% block iterable %}{% endblock %} in the for loop of feed.html as in {% for definition in {% block iterable %}{% endblock %} %} and then plugging definitions, headword.definitions_headword.all, and tag.definitions_tag.all in {% block iterable %}, but Django does not seem to support that.
I could probably pass to all three templates a "definitions" context variable and use that as the iterable in all three for loops, but is there a way to do it without touching Python code?

Your explanation was a bit fuzzy to me at the end there, but going just based off your first sentence of "Clearly, I need a base feed.html template that these three files can share."
I think {% include %} may be what you are looking for.
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#include
Create your base template of "feed.html" which includes generic variables. Generic variables below are "header_name" and "definitions".
<h1>{{ header_name }}</h1>
<ul>
{% for definition in definitions %}
// definition HTML
{% endfor %}
</ul>
In your parent templates (index, headword, tag), use the "include" tag. Pass in your parent template variables into the child template. Examples below:
headword.html
{% block body %}
{% include 'pathname/feed.html' with header_name=headword definitions=headword.definitions_headword.all %}
{% endblock %}
tag.html
{% block body %}
{% include 'pathname/feed.html' with header_name=tag definitions=tag.definitions_tag.all %}
{% endblock %}

Related

Django assignment_tag conditional

I'm trying to show partials based on a simple condition. My condition is whether an assignment_tag is True or False.
Templatetag:
from django import template
register = template.Library()
#register.assignment_tag
def partner():
return False
Template:
{% load partner_check %}
{% if partner %}
{% block header %}
{% include 'includes/partner_header.djhtml' %}
{% endblock header %}
{% block footer %}
{% include 'includes/partner_footer.djhtml' %}
{% endblock footer %}
{% endif %}
No matter what I set partner to, the blocks still appear. What am I missing?
Firstly, that's not how assignment tags work. You have never actually called the tag; if partner refers to a (non-existent) template variable named "partner". You call an assignment tag by using it on its own along with a variable to assign it to:
{% partner as partner_value %}
{% if partner_value %}...{% endif %}
Secondly, that's not how blocks work either. You can't dynamically define blocks; they are part of the basic structure of a template, not something that is assigned during evaluation.
I accomplished this by using a context_processor (https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS)
Context Processor:
def partners(context):
return {
'partner': False
}
Template:
{% block header %}
{% if partner %}
{% include 'includes/partner_header.djhtml' %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock header %}
{% block footer %}
{% if partner %}
{% include 'includes/partner_footer.djhtml' %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock footer %}

Conditionally extending a template in Django

I have a download page on a Django site that I want to serve for both users who are logged in and who aren't. Instead of having a user_download.html and login_download.html, I want to have a single download.html that conditionally extends the correct base.
However, I get an error when I use the following code.
{% if user.is_authenticated %}
{% extends 'user_base.html' %}
{% else %}
{% extends 'login_base.html' %}
{% endif %}
{% block content %}
<h2>Downloadable content</h2>
...
{% endblock %}
The error I receive is
TemplateSyntaxError at /download/
Invalid block tag: 'else'
What's wrong with the else? I tried
{% if user.is_authenticated %}
{% extends 'user_base.html' %}
{% else %}{% if AnonymousUser.is_authenticated %}
{% extends 'login_base.html' %}
{% endif %}{% endif %}
{% block content %}
<h2>Downloadable content</h2>
...
{% endblock %}
but this didn't work, either.
Thanks,
erip
The {% extends %} tag supports variables. See the doc for reference.
def my_view(request):
if request.user.is_authenicated
base_template_name = 'user_base.html'
else:
base_template_name = 'login_base.html'
# Pass base template name to the renderer
return render_to_response('your_template.html', {'base_template_name':base_template_name})
Template (please note that the value is not quoted):
{% extends base_template_name %}
...
You're getting an error because extends needs to be defined at the top of the template. extends controls template inheritance: you are basically creating a subclass from some parent class, which is why extends needs to be the first thing in the template.
Imagine writing a class, and in the __init__() you said something like
class DoesntKnowWhereToInheritFrom(object):
def __init__():
if something:
self.inherits_from(x)
else
self.inherits_from(y)
The compiler/interpreter would freak out.
The common way to do what you are trying to do here is to check for is_authenticated in the view, and then render the appropriate template.

Django flatpages template tag doesn't work with template inheritance

I have a Django (1.6) application that inherits a base template. I would like to include one of my (currently working) flatpages into the application landing page, something that the Django docs say is possible.
Here is my template:
{% extends "path/to/base.html" %}
{% load flatpages %}
{% get_flatpages as fp %}
{% block content %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
{% endblock content %}
This does not list any of the flatpages. However, if I remove the {% extends %} signal, so my code looks like this:
{% load flatpages %}
{% get_flatpages as fp %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
Everything works. I see the number of flatpages in my fp object (9) and my unordered list shows all the flatpage urls and titles.
This seems to me to be a bug in either how flatpages work, or how Django does template inheritance.
The base template (/path/to/base.html) doesn't have anything complex in it.
Django categorically says that this is possible:
When you load a custom tag or filter library, the tags/filters are only made available
to the current template – not any parent or child templates along the template-inheritance path.
For example, if a template foo.html has {% load humanize %}, a child template (e.g., one that
has {% extends "foo.html" %}) will not have access to the humanize template tags and filters.
The child template is responsible for its own {% load humanize %}.
This is a feature for the sake of maintainability and sanity.
Has anyone else noticed this bug? Is it an exception for just the built-in flatpages app?
EDIT 1:
Daniels answer is correct. The example code from the Django docs doesn't show including flatpage content within a {% block %}, so I didn't expect that it needed to be done:
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
My fault I guess. Live and learn.
The problem is that your get_flatpages tag is outside any blocks from the parent template. That means it simply won't be called.
Move it into the content block and it should work.
Just to reiterate the correct solution- get_flatpages needs to be placed inside the block where it's going to be referenced. So this will work:
{% extends "index.html" %}
{% load flatpages %}
{% block footer %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
...
{% endfor %}
And this will not work:
{% extends "index.html" %}
{% load flatpages %}
{% get_flatpages as flatpages %}
{% block footer %}
{% for page in flatpages %}
...
{% endfor %}
And yes, Django documentation isn't very clear on that.

Flask - put contents of a html page into the centre of a template page

I've only just started using Flask. I have a file with a series of links. It's generated by a separate script that returns something along the lines of this
<li>foo1 Name </li>
<li>foo2 Name </li>
<li>foo3 Name </li>
<li>foo4 Name </li>
which I save in a filewithlinks.html file in the templates folder
I want to do something like this
{% extends "template.html" %}
{% block content %}
<h2>The Links</h2>
<ul>
{% extends "filewithlinks.html">
</ul>
{% endblock %}
but when I try that it throws an error. for the {% extends "filewithlinks.html"> %}
Is there a way to do this?
You can only extend one template at a time. For everything else, use macros or include statements.
For your list, using {% include %} is just the ticket:
{% extends "template.html" %}
{% block content %}
<h2>The Links</h2>
<ul>
{% include "filewithlinks.html">
</ul>
{% endblock %}
Use include macro. Replace:
<ul>
{% extends "filewithlinks.html">
</ul>
with:
<ul>
{% include 'filewithlinks.html' %}
</ul>

Django: set default behavior in include template

I'm looking to set a default behavior in a template to be included.
I have a problem with Django template system not allowing to set variables in template (I've read about the Django Philosophy, and I understand it).
Here is my example problem:
I want to include a template to render a newsfeed:
template.html:
...
{% include "_newsfeed.html" with slicing=":20" %}
...
I would like to not be forced to enter the slicing argument, and set a default behavior, let's say ":20"
In my _newsfeed.html , I would like to do (pseudo-code, it doesn't work):
_newsfeed.html:
...
{% if not slicing %}{% with slicing=":20" %}{% endif %}
{% for content in newsfeed_content|slice:slicing %}
{# Display content #}
{% endfor %}
{% if not slicing %}{% endwith %}{% endif %}
Instead, I end up doing this below, that doesn't follow the DRY rule (and doesn't satisfy me!):
_newsfeed.html:
...
{% if not slicing %}{% with slicing=":20" %}
{% for content in newsfeed_content|slice:slicing %}
{# Display content #}
{% endfor %}
{% endwith %}{% else %}
{% for content in newsfeed_content|slice:slicing %}
{# Display content #}
{% endfor %}
{% endif %}
How should I do ?
If you want to do this via your template not your views file, you could create your own filter based on slice e.g.
from django.template.defaultfilters import slice_filter
#register.filter("slice_default", is_safe=True)
def slice_filter_20(value, arg=":20"):
return slice_filter(value, arg)

Categories