为了账号安全,请及时绑定邮箱和手机立即绑定

访问模板内的字典并管理其变量

访问模板内的字典并管理其变量

江户川乱折腾 2021-07-15 14:02:23
我需要创建一组带有 int 值的下拉框(例如:1-3),它们在 mongodb 中保存并正确读取。第一次会话后,我想将他们存储的值放在下拉列表中server.py 上的函数:@get('/my_url')def form():   #get the last entry in database, the most updated one   for my_document in db.mydb.find():    pass   return template('asset_form',**my_document)asset_form.tpl(部分):<h1>My site</h1>     <hr><h3>Asset:   <input name="name1" type="text" value="Mail Server" input disabled /> </h3>           {{dic_field1}}           {{dic_field2}}           {{my_document}}            <table style="width:100%">            <tr>            <th>Col1</th>            <th>Col2</th>            <th>Col3</th>              <th>Col4</tj>            </tr>            <td>                  <form method="POST" action="/the_post_url">            <br/>            Number of day(s):<select name = dic_field1>              %if{{dic_field1}} == 1:                <option value="1" selected >1</option>              %else:                 <option value="1">1</option>              %end              %if {{dic_field1}} == 2:                <option value="2" selected >2</option>              %else:                 <option value="2">2</option>              %end              %if {{dic_field1}} == 3:                 <option value="3" selected>3</option>              %else:                  <option value="3">3</option>              %end我可以在 python 服务器中获取值(正确打印它们)。my_document 字典有字段:dic_field1 和 dic_field2,在模板中,变量“{{my_document}}”输出错误:NameError("name 'my_document' 未定义",)其中 dic_field1 和 dic_field2 输出正确。拥有变量是不够的,因为在“if”中使用它们时,输出如下:TypeError("不可散列的类型:'set'",)
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

看来您并不真正了解变量在瓶子中的工作原理。运行原始 python 代码时,不需要大括号。只有在将数据值注入 html 时才需要它们。


也只需将结果发送到模板并在模板内处理它们。这样您就不必弄乱源代码,而只需专注于您的模板。


@get('/my_url')

def form():

   #get the last entry in database, the most updated one

   my_document = db.mydb.find()

   return template('asset_form', mydocument = my_document)

资产


%dic_field1 = mydocument['dic_field1']

%dic_field1 = mydocument['dic_field2']

%dic_field1 = mydocument['dic_field3']

<h1>My site</h1>     

<hr>

<h3>Asset:   <input name="name1" type="text" value="Mail Server" input disabled /> </h3>


           {{dic_field1}}

           {{dic_field2}}

           {{dic_field3}}


            <table style="width:100%">

            <tr>

            <th>Col1</th>

            <th>Col2</th>

            <th>Col3</th>  

            <th>Col4</tj>

            </tr>

            <td>

                  <form method="POST" action="/the_post_url">

            <br/>

            Number of day(s):<select name = {{dic_field1}}>

              %if dic_field1 == 1:

                <option value="1" selected >1</option>

              %else: 

                <option value="1">1</option>

              %end

              %if dic_field1 == 2:

                <option value="2" selected >2</option>

              %else: 

                <option value="2">2</option>

              %end

              %if dic_field1 == 3:

                 <option value="3" selected>3</option>

              %else: 

                 <option value="3">3</option>

              %end


查看完整回答
反对 回复 2021-07-27
  • 1 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号