A
A
abramoov2019-03-06 01:09:43
Django
abramoov, 2019-03-06 01:09:43

Problem with for function in django?

The fact is that I seem to be sending everything correctly to html
. But the for function, in principle, does not work 4 times (so many product positions), and the product names are not indicated,
it does not display anything
when I call {{ item }}, that's what it gives

"(QuerySet [Product: 2500.56 Gain Bolic 6000, Product: 35000.43 Fuioiuyt, Product: 4500.00 Gain Bolic 6000, Product: 9000.00 vhjvkkhvhjjhk],)"

(price and names of all products) (ignore the names),
in general, what should I do to make the for
models.py cycle work for me
class Product(models.Model):
    name = models.CharField(max_length=64, blank=True, null=True, default=None)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
    description = models.TextField(blank=True, null=True, default=None)
    is_active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "%s %s" % (self.price , self.name)

    class Meta:
        verbose_name = "Товар"
        verbose_name_plural = "Товары"

views.py
def home(request):
    item = (Product.objects.filter(is_active=True)),

    return render(request, 'landing/home.html', item.name.all())

home.html
{% block content %}
    {% include 'slider.html' %}
    {% include 'navbar.html' %}
    <div class="section">
        <div class="container">
            <div class="row">
            {{item}}

                    {% for name in item %}
                        <div class="col-lg-3">
                             <div class="view-item">
                                 <span>{{ item.name }}</span>
                             </div>
                        </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
    {% include 'footer.html' %}
{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tema_sun, 2019-03-06
@abramoov

The context must be a dictionary.

def home(request):
    item = Product.objects.filter(is_active=True)

    return render(request, 'landing/home.html', {"item": item})

What do you want to do with it item.name.all()?
Damn, the more I look, the more I do not understand what you generally want to do.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question