Template tag to update template context - django

I know how to write a template tag which updates template context. But the update is available only just after the tag it self.
Is there a way to write custom django template tag which would result in global template context update, like using TEMPLATE_CONTEXT_PROCESSORS?

The template is rendered step by step (sequentially), and obviously the context modified by a template tag would be available only after the template tag were called, not before.
Why not to use a context processor?

Ended up using django sekizai tags
By using it you can do something like this:
{% load sekizai_tags %}
<head>
<script>
{% renderblock 'googls_dfp' %}
</script>
</head>
<body>
{% block content %}
{% addtoblock 'google_dfp' %}
alert('Script is initialized before the body loads of this html document.');
{% endaddtoblock %}
{% endblock %}
</body>

Related

About parametrizing base template page in Django

I have the following problem, there is a base template "base.html" which defines default header and body information, which will be used by other pages.
The base page contains the rendered parameter, which depends on the request time in a non trivial way.
base.html
<!DOCTYPE html>
<head>
...
{% block head_stuff %} {% endblock %}
...
</head>
<body>
...
<p>Parameter that depends on the request time</p>
...
{% block body_stuff %} {% endblock %}
...
</body>
</html>
The pages that use it look like:
a.html b.html c.html
{% extends "base.html" %}
{% block head_stuff %} ... {% endblock %}
{% block body_stuff %} ... {% endblock %}
What I'd like to do is to render a.html, b.html, c.html without passing informations about that parameter to these pages in their views or templates.
In absence of inheritance, one would call render function by passing the parameter to a context, but in this case no views are used to construct the base.html.
How can I approach this problem?
A context processor is a function that takes request as an argument and returns a dictionary. The contents of this dictionary are then appended to the context of every template. So if you had a context process or
def my_example(request):
return dict(
name="steve",
dynamic_thing=some_other_function()
)
Then the templates could access these using {{ name }} and {{ dynamic_thing }}

How to add a contact form template into another template?

I'm very new to this and I'm trying to create a simple website for a company using django 2.
I'm using this template that is a single page: https://html5up.net/astral
I want to make the contact form work but I can't manage.
I've tried putting {% block %} {% endblock %} in the HTML file but it won't render the form, {% include %} renders the html file I created but not the form. I was wondering if it is possible to make the form that is already rendered work.
Thanks!
You can use Template Inheritance to do this.
You have your "base.html" parent template which has a placeholder for the form:
<html>
...
<div ... >
{% block contact-form %}
{% endblock %}
</div>
...
</html>
And your form is in "contact.html" child template:
{% extends "base.html" %}
{% block contact-form %}
<!-- contact form content -->
{% endblock %}
Then in your url patterns direct peeps to the view that renders the child "contact.html" template.
The {% extends %} tag lets the template engine know that it must first load the parent "base.html" and then fill in the appropriate block with the child "contact.html" template's content.

Django - Custom tag not working

I want to pass a model to template base.html.
I read about custom tags, and tried to execute this. It is not throwing any error, but is not working too.
My code:
base.html:
{% load staticfiles %}
{% load tags %}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul class="dropdown-menu" role="menu">
{% for league in get_my_leagues %}
<li> ddddd {{ league.league_name }}</li>
{% endfor %}
</ul>
{% block content %}
{% endblock %}
</body>
</html>
Now, tags.py:
from django.template import Library
from login.models import League
register = Library()
#register.inclusion_tag('base.html')
def get_my_leagues():
return League.objects.all()
register.tag('get_my_leagues', get_my_leagues)
When you use {% for x in y %}, this expects that y is a context variable in your template, not a template tag.
What an inclusion tag does is that it renders a template (the one you pass as argument to the inclusion_tag decorator), and inserts the result where the inclusion tag is used.
You probably want to register get_my_leagues as a simple tag instead (or an assignment tag, if you're using Django older than 1.9), and use it like this:
{% get_my_leagues as my_leagues %}
{% for league in my_leagues %}
...
{% endfor %}
guys.
I'm here just to tell that i found a solution for my problem. I'm using Context Processors to do this job.
Thank you all for answers!

Jinja2 template super functions not rendered with django

I have two very simple templates like
index.html:
<html>
<head>
</head>
<body>
{% block content %}hello{% endblock %}
</body>
</html>
and details.html
{% extends "index.html" %}
{% block content %}{{ super() }} world{% endblock %}
but when i render a view with details.html i get this error
Could not parse the remainder: '()' from 'super()'
do i need some import somewere?
(templates are rendered properly until i use the super() function)
Django 1.7 and earlier do not support Jinja natively. Unless you've done something to use Jinja, your templates should be in Django template language, and you can't use Jinja.
Django 1.8 will have support for multiple template engines, and native support for Jinja2.
In Django template language, you can use {{ block.super }} to access the content of the block from the parent template.

Twig block passed to embedded template

I'm using twig templates in a slim framework app. I have a template page.phtml which paginates an array of data and has several sub blocks intended for overriding per element specifics like paginating users, events, orders, etc.
page.phtml
<div class="page">
{% block block1 %}default content{% endblock %}
<ul>
...
</ul>
{% block block2 %}{% endblock %}
</div>
I have an event.phtml template which embeds page.phtml and adds some other content to the page; it also overrides the default page's block1 content
event.phtml
<html>
<body>
<h1>Event Page</h1>
{% embed "page.phtml" %}
{% block block1 %}event page content{% endblock %}
{% endembed %}
</body>
</html>
I have a custom event page which needs only modify a few of the event page's blocks so I extended event.phtml like so
custom_event.phtml
{% extends "event.phtml" %}
{% block block2 %}overridden value{% endblock %}
and expected block2's overridden content to show up in the page.phtml template embedded by the parent template. I can output the value of block2 in the parent template and it's there, but in the embedded template it's not. I tried explicitly passing the block in event.phtml in the embed like so
{%embed "page.phtml" %}
{% block block2 %}{{parent()}}{% endblock %}
...
{% endembed %}
But that yielded no difference. How do I get the overridden block2 from the custom_event.phtml template all the way through the extended event.phtml template and into the embedded page.phtml template?
You cannot do this.
{% embed %} technically defines a new (anonymous) template which extends the embedded template (this is how it can overwrite blocks) and gets included into the current templating. This is really just syntactic sugar for {% include %} and {% extends %} to avoid having to store this partial template in its own file and include it only once.
Twig inclusion are not extendable by child templates. Only blocks of the current template are available for extension. And event.phtml does not have any blocks.