Reputation: 191
I think I'm missing something with Flask. I want to read my dictionary which is a static file to a basic template and make it appear with Flask. Any idea why I'm getting an error? Thanks!
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def main():
return render_template('main.html, switch=switchList')
if __name__ == '__main__':
app.run(debug=True)
Here's my main.html
<body>
{% block content %}
{% for switch in switches %}
Switch Name: {{ switch.name }} <br>
- Serial: {{ switch.serial }} <br>
- Reachable at: {{ switch.ip }} <br>
<br>
{% endfor %}
{% endblock %}
</body>
here's the the dict database
switchList = [
{
"name": "C9200",
"serial": "L3143S",
"ip": "192.168.1.1",
"check": True,
"total": 28,
"up": 10,
"down": 5,
"disabled": 13,
"capacity": 35
},
{
"name": "C9300",
"serial": "ASDSADA5",
"ip": "172.168.44.2",
"check": False,
"total": 48,
"up": 32,
"down": 6,
"disabled": 10,
"capacity": 67
},
]
Upvotes: 0
Views: 421
Reputation: 24956
Assuming switchList
holds the data you want to show, change
return render_template('main.html, switch=switchList')
to
return render_template('main.html', switch=switchList)
Upvotes: 1
Reputation: 12018
Jinja will render a python object into html. Your issue is that your dictionary isn't being loaded as a python object, thus it cannot be served to your page. Solution, load your dictionary and pass through your html template:
...
# Load your dict – be sure to enter the correct path
with open('static/switchList.json', 'r') as f:
switchList = json.loads(f)
@app.route('/')
def main():
return render_template('main.html', switch=switchList)
...
Upvotes: 1