0

I have a for loop within my Django project, what I'am trying to do is the following :

If
morning_recess == True
lunch_recess == True
afternoon_recess == True

then the bootstrap tag in that field should be
<td><span class="badge badge-success">Success</span></td>
else
<td> None </td>

Here is my current code:

<table style="width:100%">
  <tr>
    <th>Student Name</th>
    <th>Morning Recess</th>
    <th>Lunch Recess</th>
    <th>Afternoon Recess</th>
    <th>Earned At</th>

  </tr>

  <tr>
     {% for i in students_recess_today %}
     {% if i.morning_recess == True %}
     <td>{{i.student_ps }}</td>
     <td><span class="badge badge-success">Success</span></td>
     <td>{{i.lunch_recess}}</td>
     <td>{{i.afternoon_recess}}</td>
     <td>{{i.created_at}}</td>
     {% else %}
     <td>{{i.student_ps }}</td>
     <td>None</td> 
     <td>{{i.lunch_recess}}</td>
     <td>{{i.afternoon_recess}}</td>
     <td>{{i.created_at}}</td>
     {% endif %}
  </tr>

{% endfor %}
</table>
</div>

The morning_recess works fine, however if i do another if statement after the following one, the order of my table gets all messed up. How do I write this correctly? Thank you

1
  • 2
    If you are intending multiple table rows, should the <tr> and </tr> be inside the {% for ...} loop? Otherwise, you need to clarify the question. Also suggest browser "view page source" to see what html actually got generated. Generating some <!-- html comments --> will help with debugging. Commented Feb 5, 2020 at 16:36

2 Answers 2

1

It's not clear what "in that field" means, because in your example you have one extra column before the morning_recess column. But you can put {% if %} statements anywhere you want in the template, e.g.:

<td>
  {% if i.morning_recess %}
    <span class="badge badge-success">Success</span> 
  {% else %}
    <span>None</span>
  {% endif %}
</td>
<td>
  {% if i.lunch_recess %}
    <span class="badge badge-success">Success</span> 
  {% else %}
    <span>None</span>
  {% endif %}
</td>
<td>
  {% if i.afternoon_recess %}
    <span class="badge badge-success">Success</span> 
  {% else %}
    <span>None</span>
  {% endif %}
</td>
...

Also as other commenters suggest, your for loop should probably wrap the rows of your table (<tr>...</tr>), not the columns (<td>).

Sign up to request clarification or add additional context in comments.

Comments

0

You have the loop partially inside your tr element. It starts a row at the beginning and for every student adds the columns and ends a row, so it ends up looking like <tr></tr></tr></tr>.

You should move your for statement outside of the row, like so:

...

{% for i in students_recess_today %}
  <tr>
    ...
  </tr>
{% endfor %}
</table>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.