looking for some solutions? You are welcome.

SOLVED: Django template multiple for loop


I have a tuple which holds multiple color and their codes:

color_list = (
    ('#CD5C5C', 'Indian Red'),
    ('#F08080', 'Light Coral'),
    ('#FA8072', 'Salmon'),

and this is the model:

class ColorList(models.Model):
    color = models.CharField(choices=color_list, max_length=10)

class Product(models.Model):
    color_list = models.ManyToManyField(ColorList)


def product_edit(request, pk):
    product = get_object_or_404(Product.objects.prefetch_related('color_list'), pk=pk)
    context = {'product': product,'color_list': color_list}
    return render(request, 'admin/product/product_edit.html', context)

Now I want to edit previously saved data in template using a form:

<div class="form-group">
     <label><strong>Color List</strong></label>
      <select name="color_list" class="form-control selectpicker" multiple  data-live-search="true" >

I want to show previously selected color with other color options, but using this template code, it generating multiple duplicate <options>. So the problem is how can I use multiple for loop in template with preselected color <options> ?

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots

No comments: