catherine
catherine

Reputation: 22808

Invalid literal for Decimal: u'3,982.75'

I'm having trouble with a localized form field not accepting localized input. Entering a number with a comma as a decimal separator triggers a validation error. How to fixed it? How to make that value valid for decimal field?

Here's my sample models.py

class BankAccount(models.Model):
    balance = models.DecimalField(max_digits=10,
        decimal_places=2, default="0.00")

In my views.py where the error trigger:

def manual_opening(request):
    if request.method == 'POST':
       opening_bal = request.POST.get('opening_bal')
       form = AddBankAccountForm(request.POST)
       if form.is_valid():
           form.cleaned_data['balance'] = Decimal(opening_bal)
           .....

When my user input a value, for ex. 3,485.78, for their opening_bal. The system trigger an error that the Decimal function not allow the operation because the number has a comma.

Upvotes: 0

Views: 2146

Answers (1)

A google search has revealed the truth! Format Localization

revenue = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)

Upvotes: 2

Related Questions