[Answered ]-How do I get all the product list that's been ordered in template

1👍

You filter with:

from django.contrib.auth.decorators import login_required

@login_required
def orders(request):
    orderitems = OrderItem.objects.filter(
        order__customer__user=request.user
    ).select_related('product')
    context = {'orderitems': orderitems}
    return render(request, 'accounts/orders.html', context)

The .select_related(…) clause [Django-doc] will avoid hitting the database each time when you fetch the .product of the orderitem.


Note: You can limit views to a view to authenticated users with the
@login_required decorator [Django-doc].

Leave a comment