【发布时间】:2019-03-11 00:20:07
【问题描述】:
我有一个远程 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...
【问题讨论】:
标签: python-3.x flask jinja2