replace block within {{ super() }}

2024/9/20 5:37:17

I have a base template which includes a block for the default <head> content. Within the head block, there's a block for the <title>.

For example, in the base file I would have:

<head>{% block head %}{% block title %}<title>An App</title>{% endblock title %}<script src="somescript.js"></script>{% endblock head %}
</head>

In the child template, I would like to include everything that was in the head block from the base (by calling {{ super()) }} and include some additional things, yet at the same time replace the title block within the super call.

Is there a way to do this without just putting a block around the rest of the head content (excluding the title) and just replacing all of that?

Answer

Don't call super. In your child template you can do:

{% extends "base.html" %}
{% block title %}<title>This is my new TITLE</title>{% endblock %}

Jinja replaces all the blocks in the parent by the ones defined in the child, if you do not provide a new definition it uses the definition in the parent. So it will render as:

<head><title>TITLE</title><script src="somescript.js"></script></head>

You call super if you want the default value of the block in the parent:

{% extends "base.html" %}
{% block title %}<title>TITLE</title>{{ super() }}{% endblock %}

And this renders as:

<head><title>TITLE</title><title>An App</title><script src="somescript.js"></script></head>

If you want to add more scripts just make a place holder block in your base template:

<head>{% block head %}{% block title %}<title>An App</title>{% endblock title %}<script src="somescript.js"></script>{% block moreScripts %}{% endblock moreScripts %}{% endblock head %}
</head>

And use it as in :

{% extends "base.html" %}
{% block title %}<title>TITLE</title>{% endblock %}
{% block moreScripts %}
<script src="somescript1.js"></script>
<script src="somescript2.js"></script>
<script src="somescript3.js"></script>
{% endblock moreScripts %}
https://en.xdnf.cn/q/72385.html

Related Q&A

Change Timezone for Date object Python

Hello I am using Pythonanywhere and when I call from datetime import *print date.today().dayIt is printing a different day than the day it is where I live (Austin, Texas). I figured it is because there…

Multiprocessing Pool - how to cancel all running processes if one returns the desired result?

Given the following Python code: import multiprocessingdef unique(somelist):return len(set(somelist)) == len(somelist)if __name__ == __main__:somelist = [[1,2,3,4,5,6,7,8,9,10,11,12,13,2], [1,2,3,4,5],…

Pandas: Product of specific columns

Finding the product of all columns in a dataframe is easy:df[Product] = df.product(axis=1)How can I specify which column names (not column numbers) to include in the product operation?From the help pa…

instagram.bind.InstagramClientError: Unable to parse response, not valid JSON

Made myself a simple Instagram client to make authenticated requests to their API. However, running it keeps throwing the following errorTraceback (most recent call last):File "request-ig-data.py&…

Using jinja to send data to Javascript

I have Python code, in which Im using jinja to send data to a template in Flask. I can access the code just find in HTML, but when I try displaying the data in Javascript, it doesnt work. For example, …

celery: Substantial drift from

I have quite a problem with celery on my distribted system. I have couple of machines among different localizations and Ive got a lot of warnings in my log files like:"Substantial drift from celer…

jinja2 link to static files

I am trying to understand how to create a link to static files in jinja2.Everything I look up relates to Flask whereas I am using just webapp2 at this stage.My main.py file looks as follows:import os i…

How to upgrade tensorflow with GPU on google colaboratory

Currently google colaboratory uses tensorflow 1.4.1. I want to upgrade it to 1.5.0 version. Each time when i executed !pip install --upgrade tensorflow command, notebook instance succesfully upgrades t…

How to print warnings and errors when using setuptools (pip)

I am using setuptools to package code such that it can be easily installed using cd project_name && pip install .During the setup process, I want to warn the user about pre-existing config file…

TypeError: not supported between instances of State and State PYTHON 3

I am trying to utilize a PriorityQueue from the queue class. However, im having issues putting custom objects into my PQ. I have implemented the __cmp__ function below:def __cmp__(self, other):return (…