EDv
EDv

Reputation: 169

how to pass data to html page using flask?

I have a small application where input is taken from the user and based on it data is displayed back on the HTML. I have to send data from flask to display on HTML but unable to find a way to do it. There's no error that I encountered.

[Here is my code:][1]

<pre>
from flask import Flask,render_template
app = Flask(__name__)

data=[
    {
        'name':'Audrin',
        'place': 'kaka',
        'mob': '7736'
    },
    {
        'name': 'Stuvard',
        'place': 'Goa',
        'mob' : '546464'
    }
]


@app.route("/")
def home():
    return render_template('home.html')

if __name__ == '__main__':
app.run(debug=True)

Upvotes: 15

Views: 50942

Answers (2)

user8806075
user8806075

Reputation:

You should pass the data to the homepage:

@app.route("/")
def home():
    return render_template('home.html', data=data)

Upvotes: 17

Plasma
Plasma

Reputation: 1961

Given that you're using Flask, I'll assume you use jinja2 templates. You can then do the following in your flask app:

return render_template('home.html', data=data)

And parse data in your HTML template:

<ul>
{% for item in data %}
    <li>{{item.name}}</li>
    <li>{{item.place}}</li>
    <li>{{item.mob}}</li>
{% endfor %}
</ul>

Upvotes: 27

Related Questions