Template Variations title not showing

45 views
Skip to first unread message

Ahmed Khairy

unread,
May 5, 2020, 7:12:15 PM5/5/20
to Django users

Hello all,

I have made a variation to an Item class in models.py and I think i got the template syntax right but apparently there are something wrong which i can't figure it out

Here is the model

class Item(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    price = models.FloatField()
    slug = models.SlugField(unique=True)
    image = models.ImageField(blank=False, upload_to='approved designs')


    def __str__(self):
        return self.title

    class Meta:
        unique_together = ('title', 'slug')

class Variation(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    title = models.CharField(max_length=120)
    image = models.ImageField(null=True, blank=True)
    price = models.FloatField(null=True, blank=True)

    def __str__(self):
        return self.title

and here is the template

                        {% if item.variation_set.all %}
                        <select class='form-control' name='size'>
                            {% for items in item.variation_set.all %}
                            <option value='{item.variation|lower}'>{{title.title|capfirst}}</option>
                            {% endfor %}
                        </select>
                        {% endif %}

oldtimer

unread,
May 21, 2020, 10:04:15 PM5/21/20
to Django users
Hello,

i was able to get the variant to show on the product page with using a form.

I cannot get the variants to display in the cart.

Only difference is my Variation model is a ManyToManyField to the item. I have also changed my variant options to numbers as the dict was expecting an integer and not a string, that is why mine shows 0 not size.

Hope this helps, maybe you can help me get the next peice.

thanks 

                    {% if item.variation_set.sizes %}
                    <select class="form-control" name=0 required>
                        {% for item in item.variation_set.sizes %}
                        <option value="" disabled selected hidden>Select Size</option>
                        <option value="{{ item.name }}">{{ item.name }}</option>
                        {% endfor %}
                    </select>
                    {% endif %}

Ahmed Khairy

unread,
May 23, 2020, 1:30:51 AM5/23/20
to Django users
Did you adjust your views to make the variations add to the cart ? 

oldtimer

unread,
May 23, 2020, 1:35:32 AM5/23/20
to Django users
I cannot get variations or I tried model forms for options as well and cannot get either to add to the cart. I can get both to display as options on the product page, but that is as far as I've made it. Need some help on how to get the views.py to get the options and post to cart.

Ahmed Khairy

unread,
May 24, 2020, 10:38:18 PM5/24/20
to Django users
Helloo,

Check my views in this question link, it might be useful 

Ahmed Khairy

unread,
May 24, 2020, 10:38:38 PM5/24/20
to Django users
Reply all
Reply to author
Forward
0 new messages