Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'Stocks' object is not iterable – Django

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 …

>Solution :

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

def stocks_mt(request):
    return render(request, 'azmt.html', {'stock': Stocks.objects.all()})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading