Jinja HTML显示索引号

时间:2019-03-11 00:20:08

标签: python-3.x flask jinja2

我有一个远程MongoDB,我想通过Flask / Jinja中的网页显示数据。我可以遍历我的Mongo文档,但是每个文档都有一个与字典中位置相关的数字。关于如何简单地显示数据而不是索引号的任何想法?

flask_app:

from flask import Flask, render_template
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb+srv://example:example@cluster0-zd456.mongodb.net/example?retryWrites=true'
mongo = PyMongo(app)

@app.route("/")
def index():
   example = mongo.db.collection.find({})
   return render_template("index.html", example=example)

index.html:

{% for ex in example %}
{{loop.index0}} {{ex['example_name']}}
{% endfor %}

index.html输出:

0 Example 
1 Example2
2 Example3
3 Example4
4 Example5

如果我遍历集合,还会显示索引号。例如,如果我改用:

index.html 2:

{% for ex in example %}
{{loop.index0}} {{ex}}
{% endfor %}

index.html输出2:

0 {
'_id': ObjectId('5c85fdsf210e245417'), 
'deal_url': 'https://www.example.com/+6+variety+pack', 
'brief_description': 'Variety Pack', 
'image_urls': ['https://www.example.com/images/skupics/crt/example.jpg'], 'images': [{'url': 'https://www.example.com/images/skupics/crt/example.jpg', 'path': 'full/e6b3c4abd87r4jkhfdkjhfc607ee04870f939067a.jpg', 'checksum': '4379fakdhfkjsfhkdjhf571e2eb'}], 
'msrp': '$43.61', 
'number': '14', 
'percent_off': '36% OFF', 
'product_name': 'Example Accessories And Samplers', 
'rating': '87 Rated', 
'sale_ends': '04/22)', 
'sale_price': ' $22.99'
} 

1 {
'_id': ObjectId('5c85fdsf210e245417'), 
'deal_url': 'https://www.example.com/+6+variety+pack', 
'brief_description': 'Variety Pack', 
'image_urls':

etc... 

0 个答案:

没有答案