1

The for loop works, but instead of adding the data to the table, it adds the data beneath the existing table.

laptop_list.html

<div class="container">
      <div class="table-responsive-sm">
      <table class="table">
     {%for laptop in laptops%}
    <div class="table-responsive-sm">
        <tbody>
          <tr>
            <td>{{laptop.laptop_id}}</td>
            <td>{{laptop.make}}</td>
            <td>{{laptop.model}}</td>
            <td>{{laptop.specification}}</td>
            <td>{{laptop.warranty}}</td>
            <td>{{laptop.condition}}</td>
            <td>{{laptop.price}}</td>
          </tr>
        </tbody>
      </div>
      </table>
      {%endfor%}
      </div>

    </div>

models.py

class Laptop(models.Model):
    make = models.CharField(max_length = 100)
    model = models.CharField(max_length = 100)
    specification = models.CharField(max_length = 100)
    warranty = models.CharField(max_length = 100)
    condition = models.CharField(max_length = 100)
    price = models.CharField(max_length = 100)
    added_by = models.ForeignKey(User, default = None, on_delete=models.CASCADE)

views.py

def laptop_list(request):
    laptops = Laptop.objects.all()
    return render(request,'laptops/laptop_list.html',{'laptops':laptops})

1 Answer 1

3

There are a couple of errors here.

You can't have a div as a child of a table element. You should remove the duplicate table-responsive-sm div inside the table.

Also, the table should only have a single tbody element. The tbody should be outside the for loop.

So:

<div class="container">
  <div class="table-responsive-sm">
    <table class="table">
      <tbody>
        {% for laptop in laptops %}
          <tr>
            <td>{{laptop.laptop_id}}</td>
            <td>{{laptop.make}}</td>
            <td>{{laptop.model}}</td>
            <td>{{laptop.specification}}</td>
            <td>{{laptop.warranty}}</td>
            <td>{{laptop.condition}}</td>
            <td>{{laptop.price}}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah thank you. It's the little ones that get you, I guess.

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.