Home > Software engineering >  {% for top in topwear.brand %} is not executing
{% for top in topwear.brand %} is not executing

Time:10-31

I want to list the items by brand so user can choose according to the brand of the product in the sidebar but somehow it is not happenning

views.py

     class home(View):
        def get (self,request):
            products=Product.objects.all()
            topwear=Product.objects.filter(Category='TW')
            bottomwear=Product.objects.filter(Category='BW')
            context={'topwear':topwear,'bottomwear':bottomwear,'products':products}
            return render(request,'store/index.html',context)

models.py

     class Product(models.Model):
      category=[
        ('TW','Top Wear'),
        ('BW','Bottom Wear'),
        ('Shoes','Shoes'),
        ('mobile','mobile'),
        ('Laptop','Laptop')
      ]
      title=models.CharField(max_length=100)
      selling_price=models.FloatField()
      discounted_price=models.FloatField()
      description=models.TextField()
      brand=models.CharField(max_length=100)
      Category=models.CharField(choices=category,max_length=10)
      product_image=models.ImageField(upload_to='productimg')
   
      def __str__(self):
        return str(self.id)

Bottom wear and mobile section is showing but not topwear section. index.html your text

     <div >
                                {% for top in topwear.brand %}
                                <a href="{% url 'searchproduct' top %}" >Top Wear</a>
                                {% endfor %}
                                <a href="" >Bottom Wear</a>
                                <a href="" >Mobile</a>
                            </div>

Everything else is running file .Just top wear section is not showing in browser. Is this syntax not correct?

Help will be appreciated

CodePudding user response:

Instead of this:

{% for top in topwear.brand %}
   <a href="{% url 'searchproduct' top %}" >Top Wear</a>
{% endfor %}

try this:

{% for top in topwear %}
  <a href="{% url 'searchproduct' top.brand %}" >Top Wear</a>
{% endfor %}

CodePudding user response:

{% for top in topwear %}
<a href="{% url 'searchproduct' top.brand %}" >Top Wear</a>
{% endfor %}
  • Related