Reputation:
I've found a shorthand method to fetch the results from MongoDb and to pass to the jinja template.
@app.route('/home')
def home():
table = mongo.db.posts
result = table.find( { } ).sort([("postdate", 1)])
records = json.loads(json_util.dumps(result))
if result.count() > 0:
return render_template('users/index.html', posts=records)
else:
message = 'I couldn't find any post'
return render_template('users/index.html', message=message)
And in users/index.html I would like to display the results like this:
{% for post in posts %}
<tr>
<td>{{post._id}}</td>
<td>{{post.title}}</td>
<td>{{post.author}}</td>
<td class="date">{{post.postdate}}</td>
</tr>
{% endfor %}
Everything works as expected except date fields. Is there a way to display date fields correctly ?
{'$date': 1508227970796}
{'$date': 1508228089163}
{'$date': 1508241780398}
Upvotes: 0
Views: 2469
Reputation: 314
What you are seeing is the timestamp of this date, there are two possibilities to solve this issue:
you ca use this inside you view to convert the date into a readable format before sending the array to the Jinja template
from datetime import datetime
datetime.fromtimestamp(the_date_you_want_to_convert)
or you can create a simple Jinja filter which will convert a given timestamp into your readable date format.
Upvotes: 0