user2086641
user2086641

Reputation: 4371

Template logic not working - Django

template

<form  method="post" action=".">
{% csrf_token %}
  <table  width="100%" cellpadding="0" cellspacing="0" >
   <tr>
   <td colspan="2" class="incident-type">
   {% for type in typeList%}
   {% if type.parent_type_id == None %} 
   <h1>{{type.title}}</h1>
   {% else %}
   {% if checked_ones  %}
   <label><input type="checkbox"  checked="True" value="{{ type.title }}" name="key">{{ type.title }}</label><br /> 
   {% else %}
   <label><input type="checkbox"  value="{{ type.title }}" name="key">{{ type.title }}</label><br /> 
   {% endif %}
{% endfor %}

see here,check box inputs are getting from this line in views.py

 checked_ones = [unicode(x) for x in subtype if unicode(x) in request.POST.getlist('key')] 

The Problem here is,if any one of the option is checked and saved,all the other checkbox options are getting selected(after page redirect).

Tried with some logic loop,Need some help.

Upvotes: 0

Views: 72

Answers (1)

Monk L
Monk L

Reputation: 3368

Try this,

{% for type in typeList%}
  {% if type.parent_type_id == None %} 
  <h1>{{type.title}}</h1>
  {% else %}
  {% if type.title in checked_ones %}
  <label><input type="checkbox"  checked="True" value="{{ type.title }}" name="key">{{ type.title }}</label><br /> 
  {% else %}
  <label><input type="checkbox"  value="{{ type.title }}" name="key">{{ type.title }}</label><br /> 
{% endif %}
 {% endif %}
  {% endfor %}

Hope this helps!

Upvotes: 2

Related Questions