I've been trying to set 'use_locale' parameter for wtform DecimalField, but nothing seems to work and there's barely any examples on how to set 'use_locale'. I've been through the documentation from both Babel and WTForm DecimalField and i got nothing so far.
Here's the relevant code snips:
init.py:
from flask import Flask, request
from config import Config
from flask_babel import Babel, Locale
app = Flask(__name__)
app.config.from_object(Config)
babel = Babel(app)
locale = Locale('pt', 'BR')
forms.py:
from flask_wtf import FlaskForm
from app import app, db, locale
from wtforms import StringField, BooleanField, SubmitField, TextAreaField, IntegerField, SelectField, DecimalField
from wtforms.validators import ValidationError, DataRequired, EqualTo, Length
class Alterar(FlaskForm):
valor = DecimalField('ValorTotal', validators=[DataRequired(),Length(min = 1)], use_locale = True)
routes.py
from flask import render_template, flash, redirect, url_for, request
from app import app, db
from app.forms import Alterar
from app.models import Orders
@app.route('/v_info/<id_order>', methods=['GET, POST'])
def v_info(id_order):
table = db.session.query(Orders).filter(Orders.id_ord == id_order).first()
form = Alterar(request.form, obj=table)
return render_template('v_info.html', data = table, form = form)
And, finally, the error that I get:
Traceback (most recent call last):
File "c:\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "c:\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "c:\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "c:\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\MS\websiteProject\app\routes.py", line 78, in v_info
form = Alterar(request.form, obj=table)
File "c:\anaconda3\lib\site-packages\wtforms\form.py", line 208, in __call__
return type.__call__(cls, *args, **kwargs)
File "c:\anaconda3\lib\site-packages\flask_wtf\form.py", line 87, in __init__
super(FlaskForm, self).__init__(formdata=formdata, **kwargs)
File "c:\anaconda3\lib\site-packages\wtforms\form.py", line 268, in __init__
super(Form, self).__init__(self._unbound_fields, meta=meta_obj, prefix=prefix)
File "c:\anaconda3\lib\site-packages\wtforms\form.py", line 51, in __init__
field = meta.bind_field(self, unbound_field, options)
File "c:\anaconda3\lib\site-packages\wtforms\meta.py", line 27, in bind_field
return unbound_field.bind(form=form, **options)
File "c:\anaconda3\lib\site-packages\wtforms\fields\core.py", line 376, in bind
return self.field_class(*self.args, **kw)
File "c:\anaconda3\lib\site-packages\wtforms\fields\core.py", line 663, in __init__
super(DecimalField, self).__init__(label, validators, **kwargs)
File "c:\anaconda3\lib\site-packages\wtforms\fields\core.py", line 588, in __init__
self.locale = kwargs['_form'].meta.locales[0]
TypeError: 'bool' object is not subscriptable
127.0.0.1 - - [26/Sep/2020 07:42:48] "[35m[1mGET /v_info/1234 HTTP/1.1[0m" 500 -
I understand that a bool object is not subscriptable, however, what am I supposed to pass in 'use_locale' parameter? Detailed info about the class from the docs:
class wtforms.fields.DecimalField(default field arguments, places=2, rounding=None, use_locale=False, number_format=None)[source]¶
A text field which displays and coerces data of the decimal.Decimal type.
Parameters
places – How many decimal places to quantize the value to for display on form. If None, does not quantize value.
rounding – How to round the value during quantize, for example decimal.ROUND_UP. If unset, uses the rounding value from the current thread’s context.
use_locale – If True, use locale-based number formatting. Locale-based number formatting requires the ‘babel’ package.
number_format – Optional number format for locale. If omitted, use the default decimal format for the locale.
If you just want to view the formatted value, you can define a filter and apply it to the template:
If you wanna add it to the input filed, maybe you can try using filters (Not sure if that works):