【问题标题】:using GridFS with Tornado templates将 GridFS 与 Tornado 模板一起使用
【发布时间】:2012-09-15 10:09:32
【问题描述】:

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

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

【问题讨论】:

    标签: tornado pymongo gridfs


    【解决方案1】:

    已解决,循环中有一个循环。 Answer on Google groups

    代码如下:

    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 %} 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2014-09-08
      • 1970-01-01
      • 2011-06-03
      相关资源
      最近更新 更多