Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 1.38 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Django/Python: Loop over selected form fields in Django template
  2. <table>
  3.             {% for field in form %}
  4.             {% if forloop.counter == 4 <<< Break here >>>%}
  5.             <tr>
  6.                 <td> {{ field.label_tag }} </td>
  7.                 <td> {{ field }} </td>
  8.             </tr>
  9.             {% endfor %}
  10.         </table>
  11.         .... Different code ....
  12.         <table>
  13.             {% for field in form %} <<< Continue here >>>
  14.             <tr>
  15.                 <td> {{ field.label_tag }} </td>
  16.                 <td> {{ field }} </td>
  17.             </tr>
  18.             {% endfor %}
  19.         </table>
  20.        
  21. fields = list(form)    
  22. part1, part2 = fields[:4], fields[4:]
  23.  
  24.  
  25. {% for field in part1 %}{{ field }}{% endfor %}
  26. ...
  27. {% for field in part2 %}{{ field }}{% endfor %}
  28.        
  29. def show_part(form,section=1):
  30.     display = ''
  31.     for id,field in enumerate(form):  
  32.          if int(section) == 1 and id > 3:
  33.              break
  34.          elif int(section) == 2 and id < 3:
  35.              continue
  36.          display += '<tr><td>'+field.label_tag+'</td>'
  37.          display += '<td>'+field+'</td></tr>'
  38.     return display
  39.        
  40. <table>
  41.     {{ form|show_part:"1" }}
  42. </table>
  43. <table>
  44.     {{ form|show_part:"2" }}
  45. </table>
  46.        
  47. <table>
  48. {% if forloop.counter <= 4 %}
  49. ... first four fields
  50. {% else %}
  51. ... other fields
  52. {% endif %}
  53.        
  54. {% if forloop.counter == 1 %}
  55. <table>
  56. {% endif %}
  57.  
  58. {% if forloop.last %}
  59. </table>
  60. {% endif %}