14

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)
Community
  • 1
  • 1
EDv
  • 159
  • 1
  • 2
  • 7
  • Can you post the code here? It makes it easier to try to answer the question, compared to a screenshot. – doctorlove Aug 03 '18 at 09:09
  • 2
    Also, we might need to see the "home.html" template you are using. You pass parameters to the `render_template` method based on the fields in the html. Without seeing that I can't help you. – doctorlove Aug 03 '18 at 09:13

2 Answers2

24

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>
Plasma
  • 1,903
  • 1
  • 22
  • 37
17

You should pass the data to the homepage:

@app.route("/")
def home():
    return render_template('home.html', data=data)
davidism
  • 121,510
  • 29
  • 395
  • 339