0

使用 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 

避免模​​板加载整个模块并使用大量内存?

4

1 回答 1

1

解决了,循环中有一个循环。 在 Google 群组上回答

这是代码:

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 %} 
于 2012-09-24T09:33:49.533 回答