我在使用 GridFS 图片制作循环时遇到问题:
class VentesHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user = self.get_secure_cookie("mechtari")
info = tornado.escape.json_decode(user)
email = info["personnel"]["email"]
produits = self.db.users.find({"personnel.email":email},{"produit_up":1, "_id":0}).distinct("produit_up")
produit_pic_id = self.db.users.find({"personnel.email":email}).distinct("produit_up.avatar.photo")
orientation = self.db.users.find({"personnel.email":email}).distinct("produit_up.avatar.orientation")
renderer = self.fs
self.render("ventes.html", produits=produits, produit_pic_id=produit_pic_id, orientation=orientation, renderer=renderer)
和模板:
{% for produit in produits %}
{% for id in produit_pic_id %}
<div class="produit">
{% import pymongo %}
{% if orientation=="portrait" %} <!-- dumb technic to avoid image stretching ^_^ -->
<span><img src="/{{renderer.get(pymongo.son_manipulator.ObjectId([id for id in produit_pic_id])).filename}}" height="200px" class="imag">
{% else %}
<span><img src="/{{renderer.get(pymongo.son_manipulator.ObjectId(id)).filename}}" width="200px"class="imag">
{% end %}
</div>
</div>
{% end %}
{% end %}
{% end %}
我得到图片重复上传产品的时间! 所以如果我上传了 2 个产品 (product_up),然后我将获得 4 个产品,全部可以切换产品图片!顺便说一下,请注意要避免的黑客(自我未定义...那么进口呢?我是否要创建另一个变量
x = pymongo.son_manipulator
避免模板加载整个模块并且使用大量内存?
求解了,循环中有一个循环。在谷歌群组上回答
这是代码:
class VentesHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user = self.get_secure_cookie("mechtari")
info = tornado.escape.json_decode(user)
email = info["personnel"]["email"]
try:
produits = self.db.users.find({"personnel.email":email}, {"produit_up":1, "_id":0}).distinct("produit_up")
renderer = self.fs
except (errors.AutoReconnect, errors.ConnectionFailure):
self.redirect("/error")
self.render("ventes.html", produits=produits, renderer=renderer)
模板:
{% for produit in produits %}
<div class="produit">
{% from bson import ObjectId %}
{% if produit["avatar"]["orientation"]=="portrait" %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"] ["photo"])).filename}}" height="300px" class="imag">
{% else %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"] ["photo"])).filename}}"width="300px"class="imag">
{% end %}
</div>
{% end %}