Search code examples
javascripthtmltornado

Iterate and print argument passed through Tornado


I am using a tornado framework which renders a html page(trial.html). Variable "pn" has a list [u'S1', u'S2'].How HTML can iterate and print the arguments passed through tornado as a list?

class Setup(tornado.web.RequestHandler):
    def get(self):
        pn= cdict[room]['panel']    
        self.render("trial.html",pn=json.dumps(pn))

The following is a part of my html code:

<li> <a href="setup?nw={{nw}}">{{(pn) }}</li></a>

I want the list to be rendered as :

S1

S2

where both S1 and S2 have individual href.But now it gets rendered as [S1', S2'] with a single href. SO how do I split the list in HTML and assign individual href to the elements in a link.


Solution

  • Three things:

    1. Don't use json.dumps, unless you need it (in your case you don't).
    2. Use for loop in your template to iterate over the list.
    3. Use documentation.

    Here's how you can iterate over a list in a template:

    {% for item in your_list %}
        <li>{{ item }}</li>
    {% end %}
    

    The above code will create an li element for every item in your_list.