使用python将变量值传递给HTML模板时的ValueError

时间:2018-06-09 11:04:06

标签: html python-3.x python-2.7

这是我的代码:

template = ("""<!DOCTYPE html>
    <html>
       <head>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <style>
             body {
             <!-- font-family: Arial -->
             }
             * {
             box-sizing: border-box;
             }
             /* The browser window */
             .container {
             border: 3px solid #f1f1f1;
             border-top-left-radius: 4px;
             border-top-right-radius: 4px;
             }
             /* Container for columns and the top "toolbar" */
             .row {
             padding: 10px;
             background: #f1f1f1;
             border-top-left-radius: 4px;
             border-top-right-radius: 4px;
             }
             /* Create three unequal columns that floats next to each other */
             .column {
             float: left;
             }
             .left {
             width: 15%;
             }
             .right {
             width: 10%;
             }
             .middle {
             width: 75%;
             }
             /* Clear floats after the columns */
             .row:after {
             content: "";
             display: table;
             clear: both;
             }
             /* Three dots */
             .dot {
             margin-top: 4px;
             height: 12px;
             width: 12px;
             background-color: #bbb;
             border-radius: 50%;
             display: inline-block;
             }
             /* Style the input field */
             input[type=text] {
             width: 100%;
             border-radius: 3px;
             border: none;
             background-color: white;
             margin-top: -8px;
             height: 25px;
             color: #666;
             padding: 5px;
             }
             /* Three bars (hamburger menu) */
             .name{
             font-size: 15pt;      
             }
             .bar {
             width: 17px;
             height: 3px;
             background-color: #aaa;
             margin: 3px 0;
             display: block;
             }
             /* Page content */
             .content {
             padding: 10px;
             }
          </style>
       </head>
       <body>
          <div class="container">
             <div class="row">
                <div class="column left">
                   <span class="dot" style="background:#ED594A;"></span>
                   <span class="dot" style="background:#FDD800;"></span>
                   <span class="dot" style="background:#5AC05A;"></span>
                </div>
                <div class="column middle">
                   <input type="text" value="Net Income">
                </div>
                <div class="column right">
                   <div style="float:right">
                      <span class="bar"></span>
                      <span class="bar"></span>
                      <span class="bar"></span>
                   </div>
                </div>
             </div>
             <div class="content">
                <!--<h3>Browser Window</h3>-->
                <!--<p> {stock_name} <p>-->
                <div class='name'>
                   <p> {{ stock_name }} </p>
                </div>
             </div>
          </div>
       </body>
    </html>
    """)
    stock_name = 'DAU'
    text = template.format(stock_name = stock_name)
    div = Div(text=text, width=200, height=100)
    show(widgetbox(div))

它给出了以下错误消息:

  

text = template.format(stock_name = stock_name)       ValueError:预期&#39;:&#39;转换说明符后

我想像这样输出:

enter image description here

我的目标是从数据库中获取值,然后显示它们。目前我开始使用静态值。

1 个答案:

答案 0 :(得分:0)

如果您使用'string'.format(...),则字符串中不得包含其他{,因为format会将所有{}替换为指定值。你必须:

  • 每张pracket taht都必须加倍:}}{{
  • 用于格式化的内容是单个:{stock_name}

示例:

print('{{ Stock name is {stock_name}.}}'.format(stock_name='ABC'))

返回{ Stock name is ABC.}