Loading

Quipoin Menu

Learn • Practice • Grow

flask / Passing Data to Templates
tutorial

Passing Data to Templates

To make your web pages dynamic, you need to pass data from your Python code to the HTML templates. Flask's `render_template` function accepts any number of keyword arguments that become available in the template.

Passing Simple Variables

@app.route('/profile')
def profile():
username = 'JohnDoe'
age = 28
return render_template('profile.html', name=username, age=age)
In `profile.html`: `

Name: {{ name }}, Age: {{ age }}

`.

Passing Lists

@app.route('/items')
def items():
product_list = ['Laptop', 'Phone', 'Tablet']
return render_template('items.html', products=product_list)
In `items.html`:
<ul>
{% for p in products %}
<li>{{ p }}</li>
{% endfor %}
</ul>

Passing Dictionaries

@app.route('/user')
def user():
user_info = {'name': 'Alice', 'city': 'New York', 'job': 'Engineer'}
return render_template('user.html', user=user_info)
In `user.html`: `

Name: {{ user.name }}, City: {{ user.city }}

`.

Passing Objects from Database (Later)

When you work with databases, you will pass model objects directly. The syntax is the same – access attributes with dot notation.


Two Minute Drill
  • Pass data as keyword arguments to `render_template()`.
  • Access them in templates using `{{ variable_name }}`.
  • You can pass lists, dictionaries, or any Python object.
  • Use `{% for item in list %}` to loop over lists.

Need more clarification?

Drop us an email at career@quipoinfotech.com