I am trying to show a list of associated "cliqueclasses" to a given "Item", but am just not able to get the template for the loop right. I'd also like to take this one step further and attach only the classes associated to the item (based on the foreign key)
I have been trying for a while and haven't had any luck so far particularly with the template!
Models.py
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
image = models.ImageField()
def save(self, *args, **kwargs):
...
def __str__(self):
return self.title
...
class CliqueClass(models.Model):
title = models.CharField(max_length=100)
start_date = models.DateTimeField(auto_now_add=True)
end_date = models.DateTimeField(auto_now_add=True)
item = models.ForeignKey(Item, related_name='items',
on_delete=models.CASCADE)
description = models.TextField()
plan = models.TextField()
slug = models.SlugField()
image = models.ImageField()
def save(self, *args, **kwargs):
...
def __str__(self):
...
My Views.py
class ItemDetailView(DetailView):
model = Item
template_name = "clique.html"
# queryset = CliqueClass.objects.filter(title=Item.title)
def get_context_data(self, **kwargs):
"""
This has been overridden to add `cliqueclass` to the template context,
now you can use {{ cliqueclass }} within the template
"""
context = super().get_context_data(**kwargs)
context['cliqueclass'] = CliqueClass.objects.all()
return context
My URL's.py
path('clique/<slug>/', ItemDetailView.as_view(), name='clique'),
My clique.html template:
{% for class in object.cliqueclass_set.all %}
<!--Look through all cliques classes-->
<div class="mb-3">
<a href="">
<span>{{ class }}</span>
<span class="badge purple mr-1">{{ class.title }}</span>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus suscipit modi sapiente illo soluta odit
voluptates,
quibusdam officia. Neque quibusdam quas a quis porro? Molestias illo neque eum in laborum.</p>
</a>
</div>
<hr class="solid">
{% endfor %}
Any help would be really appreciated :)