亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問模板內的字典并管理其變量

訪問模板內的字典并管理其變量

江戶川亂折騰 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 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號