Raymond Seger Raymond Seger - 5 months ago 17
Python Question

range(1,1) is not processed by loop

I was trying to loop product's quantity:

{% for the_number in one_product.product_stock_range %}
{% if the_number == one_product.quantity %}
<option value="{{ the_number }}" selected="selected">{{ the_number }}</option>
{% else %}
<option value="{{ the_number }}">{{ the_number }}</option>
{% endif %}
{% endfor %}


now i am using

'product_stock_range' : range(1, product_stock),


but sometimes, some product's stock is only 1. The loop will not run, how to i solve this issue? how do i force the loop to run even when it's only one-time loop?

Answer Source

You could use a conditional:

range(1, product_stock if product_stock > 1 else 2)

this, in the case that product_stock is 1, yields a range(1, 2) which results in a single iteration (which is what you say you need).