python - Django referencing Javascript variable in server side template code -


i have page adds textboxes dynamically, , on dynamically added text boxes, want access relevant context variable. so, somehow need use 'i' variable inside javascript code/template code reference it's value in context. here's javascript code need use 'i' index context list:

<script> var = 2  function add_field() {     var table = document.getelementbyid("tbl_location").getelementsbytagname('tbody')[0];      var input_name = document.createelement("th")     input_name.appendchild(document.createtextnode("s/n " + i))      var input_tb = document.createelement("td")     var input = document.createelement('input');     input.setattribute("name", "sn" + i);     {% if curr_sn.i %}     input.setattribute("value", {{ curr_sn.i }});     {% endif %}     input_tb.appendchild(input)      var input_row = document.createelement("tr")     input_row.appendchild(input_name)     input_row.appendchild(input_tb)      table.appendchild(input_row);     i++;  } 

the specific lines are:

{% if curr_sn.i %} input.setattribute("value", {{ curr_sn.i }}); {% endif %} 

where 'i' javascript veriable, , curr_sn list python code gives context.

the html part need use 'i':

{% in max_board_list %}     {% if curr_sn.i %}         <tr>             <th>s/n {{ }}</th>             <td><input type="text" name="sn{{ }}" value="{{ curr_sn.i }}"></td>         </tr>     {% endif %} {% endfor %} 

here 'i' generated loop, still need access same list.

how can in both parts of code? find the opposite way works me (using django template in javascript code).

thanks

edit: javascript side found solution (since noticed anyway code redundant , tries similar things in js&html). javascript part use:

{% if curr_sn|length > 1 %} = {{ curr_sn|length }} {% endif %} 

just keep counting current number of boards. need solve html parts display saved boards in context variable curr_sn

found solution html part well. use custom filters checking. i've registered 2 filters:

@register.filter(name='is_in') def is_in(index, list):     if list none:         return false     try:         list[int(index)]         return true     except:         return false   @register.filter(name='get_value')  def get_value(index, list):     if list none:         return false     try:         return list[int(index)]     except:         return none 

and in html part use following:

{% in max_board_list %}     {% if i|is_in:curr_sn %}         <tr>             <th>s/n {{ }}</th>             <td><input type="text" name="sn{{ }}" value="{{ i|get_value:curr_sn }}"></td>         </tr>     {% endif %} {% endfor %} 

hope others same problem.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -