[英]Django - Variable not being set properly
So I am trying to run my users input through if elif statements in some arrays I setup. 所以我试图通过我设置的某些数组中的ifif语句来运行用户输入。 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
None
. 现在,这些值保持为
None
。 I am sure this is something silly that I am missing on my part, but any help would be greatly appreciated. 我确信这是我很想念的东西,但是任何帮助将不胜感激。
views.py 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 example 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
You have a few problems here. 您在这里遇到一些问题。
Firstly, you are using the values directly from request.POST
. 首先,您直接使用来自
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. 要获取表单中指定类型的值,应使用
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")
. 由于浮点数的不精确性,例如
29.1
与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. 等等。
You are setting chest
from request.POST
which will give you a string value. 您正在从
request.POST
设置chest
,这将为您提供一个字符串值。 Convert it to a float
and it should work. 将其转换为
float
,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.