Printing lists with Mako template (Django Join tag)

廉价感情. 提交于 2019-12-14 02:27:19

问题


In this article Django templates like this

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

prints the list mylist which is an object. Can this be done in Mako? Thanks.

EDIT

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:


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

EDIT: I would suggest using an ordered list or named tuple instead of a dictionary so you can focus on your output instead of your data.

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

You can nest these if you need to output many <td>s. To do the join part, just use a nested loop.



来源:https://stackoverflow.com/questions/4061673/printing-lists-with-mako-template-django-join-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!