[Fixed]-Django – Variable not being set properly

1πŸ‘

βœ…

You have a few problems here.

Firstly, you are using the values directly from request.POST. Those will always be strings. To get the values in the type specified in your form, you should use the form.cleaned_data dict.

Secondly, your fields are decimals, but your arrays contain floats. Due to floating-point imprecision, 29.1 for example is not the same as decimal.Decimal("29.1"). You should use consistent types; Decimal is probably the most appropriate here.

Thirdly, this isn’t really the best way to do it. It would be much easier to use comparisons:

if Decimal("29.0") <= chest < Decimal("32.0"):
   ...
elif Decimal("32.0") <= chest < Decimal("33.5"):
   ...

and so on.

0πŸ‘

You are setting chest from request.POST which will give you a string value. Convert it to a float and it should work.

πŸ‘€Jody Boucher

Leave a comment