There is a Cart table with M2M connection to Product. From the created intermediate model, you need to make a selection of data and transfer them to html for display in the user's basket.

I don't understand how exactly to select, given that Django complains that M2M is not iterable and does not have a filter attribute

<div id="cart">    {% for i in cart %}
    <ul><li>{{i.product}}</li></ul>    <h1>{{i}}</h1>    {% endfor %}
def get_cart(request):
    cart= Product.cart_set
    # cart= Cart.product.all(username=request.user)
    return render(request, 'pizzahome/cart.html', {'title': 'cart', 'cart': cart})
class Cart(models.Model):
    username= models.ForeignKey(User, on_delete=PROTECT)
    creation_date= models.DateTimeField(blank=True)
    product= models.ManyToManyField(Product, null=True)
    class Meta:
        verbose_name= ('Baskets')
        verbose_name_plural= ('Baskets')
        ordering= ('-creation_date',)
    def __str__(self):
        return str(self.username)```
  • Answer # 1

    and try filter()

    cart= Cart.objects.filter(username=request.user)