0👍
✅
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).
1👍
You will need to change range(1, product_stock)
to range(0, product_stock)
or range(product_stock)
for short (the default is zero), since indexing starts at 0 for most programming languages, including Python.
👤Taku
- [Answered ]-How to resize an image in Python without using Pillow
- [Answered ]-Django/AngularJS: CSFR Token Error
- [Answered ]-Difficulty with managing the error in Django – NoReverseMatch at
- [Answered ]-Django access m2m field attribute from a queryset
1👍
I think you have a bug for all product counts, its just more pronounced when count is 1. Suppose you have 4 products, range(1, 4)
only counts 1, 2, 3
. That’s because range
is open… it doesn’t include the terminating number. You are always displaying one too few products, its just most pronounced when count is 1. The normal way to solve this is
'product_stock_range' : range(1, product_stock+1),
- [Answered ]-Django Editable Text Field
- [Answered ]-Access args in render function in django
- [Answered ]-How to update serialized data on valid?
Source:stackexchange.com