Home > OS >  'Stocks' object is not iterable - Django
'Stocks' object is not iterable - Django

Time:12-03

I saw that there are other people who have my problem, but I still can't solve the problem ... thanks to anyone who will help me!

models.py

class Stocks(models.Model):
    image = models.ImageField()
    name = models.CharField(max_length=50)
    value = models.FloatField()
    desc = models.CharField(max_length=299)
    link = models.CharField(max_length=30)

views.py

def stocks_mt(request):
    return render(request, 'azmt.html', {'stock': Stocks},)

home.html

<div class="container">
    <div class="row">
      {% for Stocks in stock %}
      <div class="col-sm">
        <br><div class="card" style="width: 18rem;">
            <img src="{{stocks.image}}" class="card-img-top" alt="...">
            <div class="card-body">
              <h5 class="card-title text-center">{stocks.name}</h5>
              <!--<h5 class="card-text text-center">50.90€</h5>-->
              <p class="card-text ">{{stocks.desc}}</p>
              <h5 class="card-text text-center">{{stocks.value}}</h5>
              <a href="{{stocks.link}}" class="btn btn-primary">Buy</a>
            </div>
          </div>
      </div>
      {% endfor %}
      </div>

I tried to capitalize the first letter but nothing has changed ...

CodePudding user response:

You need to pass a QuerySet of Stocks objects, so:

def stocks_mt(request):
    return render(request, 'azmt.html', {'stock': Stocks.objects.all()})
  • Related