Pymongo Tips – Biar Data Bisa Digunakan Berulang di Flask Template

Biasanya, ketika menggunakan query seperti ini di pymongo

data = db.collection.find({"job": "programmer"})

return render_template("template.html", data=data)

Setelah di-looping sekali, variable data akan empty [], bagaimana jika kita ingin menggunakan berulang? Cukup bungkus statement di atas menjadi:

data = [i for i in db.collection.find({"job": "programmer"})]

Dan, sekarang data bisa digunakan berulang-ulang sesuai keinginan…

Happy coding..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s