在Python的Tornado框架中连接两个字符串变量

1 投票
1 回答
2312 浏览
提问于 2025-04-18 04:57

我在查看Tornado的API时,发现我不知道怎么把两个字符串连接起来,一个是固定的,另一个是变量。Tornado似乎不支持Jinja2或Django模板的继承。

我的最终目标是用一个动态生成的变量作为GET请求的参数。我可以用一个带有隐藏变量的表单,但同时需要渲染的值有几百个。

我提供了一个例子来证明我的工作,但我也尝试过使用Django/Jinja2模板的例子。有没有什么建议呢?

<div class="row">
    <div class="small-6 columns">
        <table>
            <th>Name</th>
            <th>Broken</th>
            <th>Placed</th>
            <th>Kills</th>
            <th>Deaths</th>
            {% for player in players %}
                <tr>
                    <td><a href='/players/' + {{ player.player }}>{{player.player}}</a></td>
                    <td><a href="#">{{player.blocks_broken}}</a></td>
                    <td><a href="#">{{player.blocks_placed}}</a></td>
                    <td><a href="#">{{player.kills}}</a></td>
                    <td><a href="#">{{player.deaths}}</a></td>
               </tr>
           {% end %}
       </table>
   </div>
</div>

这会生成以下的URL:localhost:8888/players

我想要的输出是:localhost:8888/players/bob

1 个回答

2

为什么不直接把那行写成:

<td><a href='/players/{{ player.player }}'>{{player.player}}</a></td>

这样就不需要拼接了,它会在到达浏览器之前就构建好。

撰写回答