miguels
miguels

Reputation: 636

Accessing dictionary inside template and managing its variables

I need to create group of dropdown boxes with int values (ex: 1-3), they're saved and read properly in a mongodb. After a first session i'd want to put their stored values in the dropdown

The function on the 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)

The asset_form.tpl (part of):

<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

I can get the values in the python server (printed them properly). The my_document dictionary has the fields: dic_field1 and dic_field2,

In the template the variable "{{my_document}}" outputs an error:

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

Where as dic_field1 and dic_field2 output properly.

Having the variables isen't enough because when using them in the "if" the output is the following:

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

Upvotes: 0

Views: 173

Answers (1)

eatmeimadanish
eatmeimadanish

Reputation: 3907

It appears you don't really understand how the variables work in bottle. When running raw python code, you do not need the braces. You only need them when injecting the data values into the html.

Also just send the results to the template and deal with them inside of the template. That way you don't have to mess with source code, and just focus on your template.

@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)

Asset

%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

Upvotes: 2

Related Questions