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

2024-03-29 15:17:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一组带有int值的下拉框(例如:1-3),它们在mongodb中被正确保存和读取。在第一次会话之后,我想把它们存储的值放到下拉列表中

上的函数服务器.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)

资产_表格.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服务器中获得值(正确地打印它们)。 “我的文档词典”有字段:dic\u field1和dic\u field2

在模板中,变量“{myu document}}”输出一个错误:

NameError("name 'my_document' is not defined",)

其中as dic\ U field1和dic\ U field2输出正确。你知道吗

拥有这些变量是不够的,因为在“if”中使用它们时,输出如下:

TypeError("unhashable type: 'set'",)


Tags: thenameformifvaluemydocumentelse
1条回答
网友
1楼 · 发布于 2024-03-29 15:17:48

看来你不太明白这些变量在瓶子里是怎么工作的。运行原始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

相关问题 更多 >