Computer scienceBackendFlaskTemplates

Jinja filters

Random

Report a typo

Matt builds a simple web application that displays quotes from his favorite classical books. He scrapes quotes from other sites and put them into a list. Afterward, he wants to display a single quote to be chosen at random from the list and displayed in the homepage.

He tried to create a simple filter called pick_random. He puts {{ quote_list | pick_random }} in the template, yet he kept getting the error No filter named 'pick_random'. Help Matt fix the issue in the code below.

Write a program in Python 3
from flask import Flask,render_template_string
import random

app = Flask(__name__)

def pick_random(lst):
indx = random.randint(0,len(lst)-1)
return lst[indx]

@app.route("/quotes")
def quotes():

return render_template_string("""

{{ ["quote1","quote2","quote3"] | pick_random }}

""")
___

Create a free account to access the full topic