使用Mako模板打印列表(Django Join标签)

时间:2010-10-31 03:07:44

标签: mako

In this article像这样的Django模板

{% for i in mylist %}
  <tr>
    <td>{{i.replist|join:"</td><td>" }}</td>
  </tr>
{% endfor %}

打印作为对象的列表mylist。这可以在Mako完成吗?感谢。

修改

class Rep(db.Model):
    author = db.UserProperty()
    replist = db.ListProperty(str)
    unique = db.ListProperty(str)
    date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
    def get(self):      
        user = users.get_current_user()
        greeting = None

        if user:
            greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
                         (user.nickname(), users.create_logout_url("/")))
        else:
            greeting = ("<a href=\"%s\">Sign in or register</a>." %
                        users.create_login_url("/"))

        L = []                                   
        s = self.request.get('sentence')           
        L.append(s)                              

        L = L[0].split('\r\n')     

        def f2(L):
            checked = []
            for e in L:
                if e not in checked:
                    checked.append(e)
            return checked

        Rep().replist = L                                   
        Rep().put()                              
        mylist = Rep().all().fetch(10)

        leng = len(mylist)
        T = type(mylist)
        self.response.out.write("Ttttt")
        print [i for i in mylist]       
        L2 = f2(L)
        x = len(L)
        y = len(L2)
        delta = x - y
        for i in range(delta):
             L2.append('')

         q = Rep().all()
         results = q.fetch(10)
        db.delete(results)



         template_values = {"s": s,
                           "L": L,
                           "L2": L2,
                           "x": x,
                           "y": y,
                           "greeting": greeting,
                           "mylist": mylist,
                           "leng": leng,
                           "T": T,
                           }

        path = os.path.join(os.path.dirname(__file__), 'main.mako')
        templ = Template(filename=path)
        self.response.out.write(templ.render(**template_values))  

1 个答案:

答案 0 :(得分:1)

http://www.makotemplates.org/docs/syntax.html#syntax_control

编辑: 我建议使用有序列表或命名元组而不是字典,这样您就可以专注于输出而不是数据。

% for entry in mylist:
    <tr>
        % for key, value in entry:
        <td>${value}</td>
        % endfor
    </tr>
% endfor

如果需要输出多个<td>,可以嵌套这些。要执行连接部分,只需使用嵌套循环。