Variable substitution lets you show dynamic data inside your web pages easily. It replaces placeholders with real values when the page loads.
Variable substitution syntax in Flask
{{ variable_name }}Use double curly braces {{ }} to insert variables.
Variables come from the Flask code you send to the template.
username in the page.{{ username }}age property of the user object.{{ user.age }}{{ 5 + 3 }}{{ 'Hello ' + username }}This Flask app shows how to use variable substitution to greet a user by name. The variable name is replaced with the value of user_name when rendering the page.
from flask import Flask, render_template_string app = Flask(__name__) @app.route('/') def home(): user_name = 'Alice' html = ''' <html lang="en"> <head><title>Welcome</title></head> <body> <h1>Welcome, {{ name }}!</h1> <p>We are glad to see you.</p> </body> </html> ''' return render_template_string(html, name=user_name) if __name__ == '__main__': app.run(debug=False, port=5000)
Variable substitution is safe by default: Flask escapes HTML to prevent security issues.
You can use filters like {{ variable|upper }} to change output style.
Common mistake: forgetting to pass the variable from Flask to the template causes empty output.
Use {{ variable }} to insert dynamic data in Flask templates.
Variables come from your Flask code when rendering the template.
This makes your web pages interactive and personalized.
Practice
username into the HTML?Solution
Step 1: Understand Flask template syntax
Flask uses Jinja2 templating where variables are inserted using double curly braces{{ variable }}.Step 2: Identify correct variable substitution
The correct way to show the value ofusernameis{{ username }}.Final Answer:
{{ username }} -> Option BQuick Check:
Variable substitution = {{ username }} [OK]
- Using {% %} which is for control statements
- Using brackets like [[ ]] which is not Flask syntax
- Using <% %> which is from other template engines
age inside a Flask template?Solution
Step 1: Recall Flask variable syntax
Flask templates use Jinja2 syntax where variables are shown with double curly braces{{ variable }}.Step 2: Match the correct option
Only{{ age }}correctly shows the variable value.Final Answer:
{{ age }} -> Option AQuick Check:
Variable display = {{ age }} [OK]
- Confusing {% %} for variables instead of control blocks
- Using syntax from other template engines like <%= %>
- Using brackets [[ ]] which Flask does not recognize
Hello, {{ user }}!And the Flask code:
return render_template('template.html', user='Alice')What will be the rendered output?
Solution
Step 1: Understand variable substitution in template
The template uses{{ user }}which will be replaced by the value passed from Flask code.Step 2: Check the passed variable value
The Flask code passesuser='Alice', so{{ user }}becomes 'Alice'.Final Answer:
Hello, Alice! -> Option CQuick Check:
Variable user = Alice [OK]
- Thinking {{ user }} prints literally
- Ignoring the passed variable value
- Confusing variable name with string
Hello, {% user %}!What will happen when rendering this template?
Solution
Step 1: Understand {% %} usage in Flask templates
The {% %} syntax is for control statements like loops or conditions, not for variable substitution.Step 2: Identify the error type
Using {% user %} is invalid and causes a TemplateSyntaxError because 'user' is not a valid control tag.Final Answer:
TemplateSyntaxError due to wrong tag usage -> Option AQuick Check:
Wrong tag usage = TemplateSyntaxError [OK]
- Using {% %} for variables instead of {{ }}
- Expecting literal output from {% %} tags
- Ignoring template error messages
first_name and last_name. Which template code correctly combines them with a space?Solution
Step 1: Recall string concatenation in Jinja2 templates
Jinja2 uses the tilde (~) operator to join strings inside {{ }}.Step 2: Evaluate each option
{{ first_name ~ ' ' ~ last_name }} uses~correctly to joinfirst_name, a space, andlast_name. {{ first_name }} + ' ' + {{ last_name }} treats + as literal text. {{ first_name, ' ', last_name }} creates a tuple with comma. {{ first_name & ' ' & last_name }} uses invalid&operator.Final Answer:
{{ first_name ~ ' ' ~ last_name }} -> Option DQuick Check:
String join in Jinja2 = ~ operator [OK]
- Placing + outside {{ }} so it prints literally
- Using invalid operators like & or commas
- Trying Python code directly in templates
Ready to prove it?
Pass the test and this pattern moves unknown
