Python Bottle服务器模板返回格式错误的HTML代码

时间:2015-11-19 04:42:57

标签: python html bottle

所以在下面的瓶子服务器路线中,一切正常......

@route('/IFC/config/<policy>')
def config(policy):
    if policy == "test":
        html_nodes = ""
        Nodes = IFC_main.get_nodes(ipaddr,username,password)
        for node in Nodes:
            html_nodes += '<li>'+node["name"]+'</li>'
        return template("mgmt.tpl",html_nodes = html_nodes)

除非我查看产生它的网页的源代码,否则它应该是一个带有我提供的值的下拉菜单,但我得到了这个......

<script language="JavaScript" type="text/javascript" src="/static/mgmt.js">               </script>
<link rel="stylesheet" type="text/css" href="/static/mgmt.css">

<body style = "background-color:#CCCCCA">
<img id="banner" style = "bg-color:CBCCCE" src="static/Cisco_emailHeader.png" alt="Banner Image"/>
<div style="width: 800px;height: 100px;position: absolute;top:0;bottom: 0;left: 0;right: 0;margin: auto;">
<h1>Management Connectivity</h1>
 <ul class="dropdown-menu">
 &lt;li&gt;calo2-leaf3&lt;/li&gt;&lt;li&gt;calo2- spine1&lt;/li&gt;&lt;li&gt;calo2-leaf2&lt;/li&gt;&lt;li&gt;calo2- leaf1&lt;/li&gt;&lt;li&gt;apic2&lt;/li&gt;&lt;li&gt;calo2- spine2&lt;/li&gt;&lt;li&gt;apic1&lt;/li&gt;&lt;li&gt;apic3&lt;/li&gt;
</ul>
</div>
</body>

我理解我需要转换我传递给模板的字符串,但我还是无法弄清楚是什么。我假设其他人遇到了这个问题。

2 个答案:

答案 0 :(得分:1)

您可以使用感叹号启动语句,以禁用该语句的转义:

class Schedule_Label(tk.Label):
    def __init__(self, parent, *args, **kwargs):
        tk.Label.__init__(self, parent, *args, **kwargs)
        self.bind("<Button-1>", lambda event, p=parent: mouse_1(event, p)) # ---  new ---

...
class Schedule, this class uses Schedule_Label

class MainApp(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        #class schedule is used here
        ...
        self.schedule_widgets = self.schedule.New(date, stafflist) #------------  new ---
        ...

def mouse_1(event, parent): #---------------------------------------------------  new ---
    r = event.widget.grid_info()['row']
    c = event.widget.grid_info()['column']
    parent.schedule_widgets[(r,c)].configure(state="active") #------------------  new ---
if __name__ == "__main__":
    root = tk.Tk()
    app = MainApp(root)
    app.pack()
    root.mainloop()

答案 1 :(得分:0)

您需要将循环移动到模板文件中,

% for node in nodes:
    <li>{{ node.name }}</li>
% end

代码将更改为,

@route('/IFC/config/<policy>')
def config(policy):
    if policy == "test":
        nodes = IFC_main.get_nodes(ipaddr,username,password)
        return template("mgmt.tpl", nodes=nodes)