Django - Variable not being set properly
<p>So I am trying to run my users input through if elif statements in some arrays I setup. The goal is to have the variable set to a value if the input is found in the array. Right now the values are staying <code>None</code>. I am sure this is something silly that I am missing on my part, but any help would be greatly appreciated.</p>

<p>views.py</p>

<pre><code>def fit_guide(request):
    if request.method == 'POST':
        form = FitGuideSizingForm(request.POST)
        chest = request.POST['chest']
        waist = request.POST['waist']
        hip = request.POST['hip']

        if form.is_valid():

            chest_size = None

            if chest in chest_size_0():
                chest_size = 0

            elif chest in chest_size_2():
                chest_size = 2

            elif chest in chest_size_4():
                chest_size = 4

            elif chest in chest_size_6():
                chest_size = 6

            elif chest in chest_size_8():
                chest_size = 8

            elif chest in chest_size_10():
                chest_size = 10

            elif chest in chest_size_12():
                chest_size = 12

            elif chest in chest_size_14():
                chest_size = 14

            elif chest in chest_size_16():
                chest_size = 16

            elif chest in chest_size_18():
                chest_size = 18

            elif chest in chest_size_20():
                chest_size = 20

            elif chest in chest_size_22():
                chest_size = 22

            elif chest in chest_size_24():
                chest_size = 24

            elif chest in chest_size_26():
                chest_size = 26

            elif chest in chest_size_28():
                chest_size = 28

            print(chest_size)

    form = FitGuideSizingForm()
    c = {}
    c.update(csrf(request))
    c.update({'form': form})

    return render(request, 'main/fit_guide.html', c)
</code></pre>

<p>forms.py</p>

<pre><code>class FitGuideSizingForm(forms.Form):
    chest = forms.DecimalField(min_value=29, max_value=60, required=True)
    waist = forms.DecimalField(min_value=23, max_value=50, required=True)
    hip = forms.DecimalField(min_value=34.5, max_value=58, required=True)

    def __init__(self, *args, **kwargs):
        super(FitGuideSizingForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-fitGuideForm'
        self.helper.form_class = 'blueForms'
        self.helper.form_method = 'post'
        self.helper.form_action = 'submit_survey'
        self.helper.add_input(Submit('submit', 'Get Fit Guide!'))
</code></pre>

<p>size_arrays.py example</p>

<pre><code>def chest_size_0():
    return [29, 29.1, 29.2, 29.3, 29.4, 29.5, 29.6, 29.7, 29.8, 29.9,
            30, 30.1, 30.2, 30.3, 30.4, 30.5, 30.6, 30.7, 30.8, 30.9,
            31, 31.1, 31.2, 31.3, 31.4, 31.5, 31.6, 31.7, 31.8, 31.9]


def chest_size_2():
    return [32, 32.1, 32.2, 32.3, 32.4, 32.5, 32.6, 32.7, 32.8, 32.9,
            33, 33.1, 33.2, 33.3, 33.4]


def chest_size_4():
    return [33.5, 33.6, 33.7, 33.8, 33.9, 34, 34.1, 34.2, 34.3, 34.4]


def chest_size_6():
    return [34.5, 34.6, 34.7, 34.8, 34.9, 35, 35.1, 35.2, 35.3, 35.4]

etc

etc
</code></pre>

<p>You have a few problems here.</p>

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

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

<p>Thirdly, this isn't really the best way to do it. It would be much easier to use comparisons:</p>

<pre><code>if Decimal("29.0") &lt;= chest &lt; Decimal("32.0"):
   ...
elif Decimal("32.0") &lt;= chest &lt; Decimal("33.5"):
   ...
</code></pre>

<p>and so on.</p>

python django