0
0
Flaskframework~20 mins

Render_template function in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Render Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Flask route render?
Given this Flask route, what will the user see when visiting /hello?
Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/hello')
def hello():
    return render_template('greet.html', name='Alice')
AThe content of greet.html with the variable 'name' replaced by 'Alice'.
BA plain text response saying 'Hello Alice'.
CAn error because 'name' is not defined in the template.
DA JSON response with {'name': 'Alice'}.
Attempts:
2 left
💡 Hint
Think about what render_template does with the variables passed to it.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this render_template usage
What is wrong with this Flask route code?
Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/user')
def user():
    return render_template('user.html', username=)
Arender_template cannot be used inside a route function.
BMissing value for 'username' keyword argument in render_template.
CThe template file 'user.html' must be a Python file.
DFlask app must be run with debug=True to use render_template.
Attempts:
2 left
💡 Hint
Check how variables are passed to render_template.
state_output
advanced
2:00remaining
What is the output of this Flask route with conditional rendering?
Consider this route and template. What will the user see when visiting /status?
Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/status')
def status():
    user_logged_in = False
    return render_template('status.html', logged_in=user_logged_in)

# status.html content:
# {% if logged_in %}
# <p>Welcome back!</p>
# {% else %}
# <p>Please log in.</p>
# {% endif %}
A<p>Please log in.</p>
BAn empty page with no content.
CAn error because 'logged_in' is not a boolean.
D<p>Welcome back!</p>
Attempts:
2 left
💡 Hint
Look at the value of 'user_logged_in' and the template's if condition.
🔧 Debug
advanced
2:00remaining
Why does this Flask app raise a TemplateNotFound error?
This Flask route raises a TemplateNotFound error. What is the most likely cause?
Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/page')
def page():
    return render_template('mypage.html')
AThe route function must return a string, not render_template.
BFlask app must be run with debug=True to find templates.
CThe template file 'mypage.html' is missing from the 'templates' folder.
Drender_template requires the template file to be in the static folder.
Attempts:
2 left
💡 Hint
Check where Flask looks for template files by default.
🧠 Conceptual
expert
3:00remaining
How does Flask's render_template handle variable scope?
When you pass variables to render_template, how are they accessed inside the template?
AVariables are only accessible inside Python code blocks, not in HTML parts.
BVariables must be declared global in the template to be accessed.
CVariables passed are converted to JSON strings and must be parsed in the template.
DVariables passed as keyword arguments become available as template variables with the same names.
Attempts:
2 left
💡 Hint
Think about how templates use placeholders for variables.