How do I format a date in Jinja2?

There are two ways to do it. The direct approach would be to simply call (and print) the strftime() method in your template, for example {{ car.date_of_manufacture.strftime(‘%Y-%m-%d’) }} Another, sightly better approach would be to define your own filter, e.g.: from flask import Flask import babel app = Flask(__name__) @app.template_filter() def format_datetime(value, format=”medium”): if format … Read more

How can I update a .yml file, ignoring preexisting Jinja syntax, using Python?

The solution in this answer has been incorporated into ruamel.yaml using a plugin mechanism. At the bottom of this post there are quick-and-dirty instructions on how to use that. There are three aspects in updating a YAML file that contains jinja2 “code”: making the jinja2 code acceptable to the YAML parser making sure the acceptable … Read more

Rendering a dictionary in Jinja2

Your url_list should look like this: url_list = [{‘target’: ‘http://10.58.48.103:5000/’, ‘clicks’: ‘1’}, {‘target’: ‘http://slash.org’, ‘clicks’: ‘4’}, {‘target’: ‘http://10.58.48.58:5000/’, ‘clicks’: ‘1’}, {‘target’: ‘http://de.com/a’, ‘clicks’: ‘0’}] Then using: <li>{{ item[“target”] }}</li> in your template will work. Edit 1: Your template think you’re passing a list in, so are you sure you’re passing in your original dict and … Read more