0

I have written a simple script, but when I have the loop in the script the products and allbrands variable comes out empty except when I remove the loop. Here is part of the script.

products = amazon.search(Brand="Microsoft", SearchIndex="Software", ResponseGroup="Images,ItemAttributes,Accessories,Reviews,VariationSummary,Variations")
allbrands = Brand.objects.all();
for i, product in enumerate(products):
    print ("Product");
context = {
    'products': products,
    'allbrands': allbrands
}
return render(request, 'storefront/index.html', context)

1 Answer 1

2

products is probably an iterator, not an actual list. Your 'for' loop consumed all of the iterator's items, leaving none to be rendered to your page. Putting products = list(products) after the search() would be one solution - this turns the iterator into a list, which you can then iterate over as many times as you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.