I’ve built a web app with Django. Although the FloatField was designated as ‘weight’ & ‘height’ model fields, my application refused to input the float value, and only accepts integer. I attempt to declare the FloatField at the forms, but it doesn’t seem to work. I have no idea what’s cause and desire to know how to be able to input the float value with solving this problem.
Following is my code and Django is version 2.0
# Model.py
class Profile(models.Model):
class Meta(object):
db_table='profile'
user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,
primary_key=True)
weight=models.FloatField(_('weight'),blank=False)
height=models.FloatField(_('height'),blank=False)
bmi=models.FloatField(_('BMI'))
def __str__(self):
return self.user.nickname
# Form.py
class BioRegisterForm(forms.ModelForm):
class Meta:
model=Profile
fields=('weight','height')
help_texts={'weight':('Kg単位で入力してください'),'height':('cm単位で入力して下さい')}
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['weight'].widget.attrs={'placeholder':'Weight','required':True}
self.fields['height'].widget.attrs={'placeholder':'Height','required':True}
def clean_hight(self):
value=self.cleaned_data.get('height')
if value<50:
raise forms.ValidationError('Type in with 「cm」')
return value
# View.py
class BioRegisterView(CreateView):
model=Profile
template_name='mysite/bioregister.html'
form_class=BioRegisterForm
success_url=reverse_lazy('mysite/profile.html')
and the following is the bioregister.html, page in question
<form method='post' action="">
{% for field in form %}
<div>
<div class='entry'>
{{field}}
</div>
{%if field.errors %}
<p> {{field.errors.0}}</p>
{% endif %}
</div>
{% endfor %}
{% csrf_token %}
<input type="submit" value="Register" class='bottun2'>
</form>
Thank you
