Here's a Flask app which contains a list of objects.
from flask import *
app = Flask(__name__)
class Entry:
def __init__(self, name, surname):
self.name = name
self.surname = surname
entries = []
entries.append(Entry('Paul', 'McCartney'))
entries.append(Entry('John', 'Lennon'))
entries.append(Entry('George', 'Harrison'))
entries.append(Entry('Ringo', 'Starr'))
@app.route('/')
def index():
return render_template('index.html', entries = entries)
if __name__ == '__main__':
app.run(debug=True)
Here's the index.html file:
<html>
<head>
</head>
<body>
<script>
var entries = "{{entries[0].name}}"
console.log(entries)
</script>
</body>
</html>
In this case, everything works fine and console output is Paul as expected.
But when I change the code to this,
var entries = "{{entries}}"
console.log(entries[0].name)
The console outputs undefined
If we say console.log(entries) in this case, it returns the following:
[<__main__.Entry object at 0x1065a2278>, <__main__.Entry object at 0x1065a22b0>,
<__main__.Entry object at 0x1065a22e8>, <__main__.Entry object at 0x1065a2208>]
I assume that it's necessary to convert this list of objects to JS format somehow, am I right? How to do this properly? Many thanks!