Django - 变量未正确设置

时间:2016-10-04 15:30:50

标签: python django

所以我试图通过我设置的某些数组中的elif语句来运行我的用户输入。目标是如果在数组中找到输入,则将变量设置为值。现在价值保持None。我确信这对我来说是个愚蠢的事情,但是我会非常感激任何帮助。

views.py

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)

forms.py

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!'))

size_arrays.py示例

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

2 个答案:

答案 0 :(得分:1)

这里有一些问题。

首先,您直接使用request.POST中的值。那些将永远是字符串。要获取表单中指定类型的值,您应该使用form.cleaned_data dict。

其次,您的字段是小数,但您的数组包含浮点数。由于浮点不精确,29.1例如与decimal.Decimal("29.1")不同。你应该使用一致的类型;十进制可能是最合适的。

第三,这不是最好的方法。使用比较会容易得多:

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

等等。

答案 1 :(得分:0)

您正在chest设置request.POST,它会为您提供字符串值。将其转换为float,它应该有效。