Django Form: error Enter a number

Hi

I have a form where user can enter float or int
like, 1000 or 1,000.10. the problem when submit the form with a float it raises error Enter a number

 asset = models.FloatField( default=0, verbose_name=_("Total Assest")) 
<input 
type="number" 
name="{{form.asset.html_name}}" 
id="{{form.asset.id_for_label}}" 
class="form-control 
{% if form.asset.errors %}is-invalid{% endif %}" 
value="{{form.asset.value|unlocalize|default_if_none:''
|floatformat:2|intcomma}}" 
step="0.01"> 

settings

USE_I18N = True

USE_TZ = True

USE_L10N = True

USE_THOUSAND_SEPARATOR = True

i need to let the user enter int or float.

What does your form field look like for this form?

Are you getting this error in server-side validation or from the browser?
(Note that the thousand’s separator is not considered part of a numeric value. It is not legal to enter 1,000.10, you would need to enter it as 1000.10. If you need “1,000.10” to be accepted as a valid entry, you need to find a JavaScript widget to support that.)

See <input type="number"> - HTML: HyperText Markup Language | MDN

1 Like

The error is in front-end.

That’s a good note about USE_THOUSAND_SEPARATOR.

I need a way to let the user add float or int in the same input.

at /edit/ values are shown to user like 1,000.00 but if he try to save the form, it raises front-end error Enter a number.

values without ‘,’ are excepted and the form can be updated. (user must delete the THOUSAND_SEPARATOR which are send from the back-end )

If you need to allow the commas as input, then you need to change the field to be a CharField and not a numeric field. You can then use JavaScript to perform front-end verification of the entered field along with performing the validation in your view.

i think the beat way is to remove the ‘,’ before save the form. My model is asset = models.FloatField

I fount this question, but i can’t remove the ‘,’

class ReportUpdateForm(forms.ModelForm):
    year = forms.ModelChoiceField(queryset=Year.objects.none(), disabled=True)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['year'].queryset = Year.objects.filter(name__gte=self.instance.org.since)

# below is me trying to delete the ',' before saving the form
        
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['asset', 'donation', 'expense']
            print('fields', self.fields)
            return float(self.fields.replace(',', ''))

# or
    def clean_(self):
        fields = self.cleaned_data['asset', 'donation', 'expense']
        print('fields', fields)
        return float(fields.replace(',', ''))
            

    class Meta:
        model = Report
        fields = ['asset', 'donation', 'expense', 'year']

When you’re getting this error, is it posting the form to the server? (Do you see the POST in your console log?)

If insert 35,000.00 that shows error Enter a number.
Nothing is saved to DB and there is no error in the logs or console

Enter a number error

So, if it’s not posting back to the server, why do you think that making any changes in the view is going to help this situation? Nothing is being submitted, no code in the server is going to execute.

i can’t use CharField

i need a way to remove the ‘,’ so if user insert 35,000.00 that well saved as 35000.00

Why not?

To be clear, I’m talking about forms.CharField (or the TextInput widget, or type="text"), not models.CharField.

Again, your issue here is not with “trying to save the comma”. Your immediate problem is that “35,000” is not a valid type="number" input in your input field. You’re not even at the point where it’s trying to save this to the database.

Side note: I sincerely hope you’re not trying to use a FloatField for a monetary value. That’s a huge mistake, and the reason why Django (and databases) provide a DecimalField.

1 Like

OK well do models.DecimalField it’s monetary values.There are many math functions for this field.

my immediate problem is that “35,000” is not a valid type="number" input in the input field.

Q. how can i remove the comma if user add it, Because without comma “35000.00”, input well accept and save.

I’ve already answered this. See my previous answers above.