'Wtforms: filling a form automatically by clicking a button that triggers a function

Hello I am trying to make a button that triggers a function populate a form with a generated username and password, I have create a class that takes care of the generation but I am having difficulties to figure out how to automatically fill the form once I click the button, I am trying to replicate an old website for the new version of it that I am making.

Old Website(The "Magic wand" generates the password/user and fills the form): Empty Form

filled Form

this is the new website I am working on with the forms and the buttons: New Forms and Buttons

And this is my code:

class RegistraPrivatista(FlaskForm):
    cognome = StringField('Cognome: ', validators=[DataRequired(), Length(max=30)])
    nome = StringField('Nome: ', validators=[DataRequired(), Length(max=30)])
    data_nascita = DateField('Data Di Nascita: ', format='%Y=%m-%d', validators=[DataRequired()])
    telefono = IntegerField('Telefono: ', validators=[DataRequired()])
    email = EmailField('E-Mail: ', validators=[Email(), DataRequired()])
    codice = StringField('Codice', validators=[DataRequired(), Length(max=6)])
    pr = StringField('Pr', validators=[DataRequired(), Length(max=1)])
    aut = StringField('Aut', validators=[DataRequired(), Length(max=4)])
    marca = StringField('Marca Op', validators=[DataRequired(), Length(max=10)])
    data_emi = DateField('Data emiss.', format='%Y=%m-%d', validators=[DataRequired()])
    data_scad = DateField('Data scad.', format='%Y=%m-%d', validators=[DataRequired()])
    patente = SelectField('Patente', choices=[('Selezionare La Patente'),
                                            ('A1a (A1 Automatica)'),
                                              ('A2a (A2 Automatica)'),
                                              ('A3 (Motocicli senza limiti)'),
                                              ('A1m (A1)'),
                                              ('A2m (A2)'),
                                              ('B (Autoveicoli)')],
                          validators=[DataRequired()])
    nome_utente = StringField('Nome Utente', default='test', validators=[DataRequired(), Length(min=8, max=8)])
    password_utente = PasswordField('Password', validators=[DataRequired(), Length(min=8, max=8)])
    submit = SubmitField('Submit')



@app.route('/registrazione-privatista', methods=['GET', 'POST'])
def registra_privatista():
    form = RegistraPrivatista()
    cognome = None
    nome = None
    data_nascita = None
    telefono = None
    email = None
    codice = None
    pr = None
    aut = None
    marca = None
    data_emi = None
    data_scad = None
    patente = None
    nome_utente = None
    password_utente = None
    generator = PasswordGenerator()
    if form.validate_on_submit():
        cognome = form.cognome.data
        nome = form.nome.data
        data_nascita = form.data_nascita.data
        telefono = form.telefono.data
        email = form.email.data
        codice = form.codice.data
        pr = form.pr.data
        aut = form.aut.data
        marca = form.marca.data
        data_emi = form.data_emi.data
        data_scad = form.data_scad.data
        patente = form.patente.data
        nome_utente = form.nome_utente.data
        password_utente = form.nome_utente.data

    return render_template("registra_privatista.html", form=form,
                           cognome=cognome,
                           nome=nome,
                           data_nascita=data_nascita,
                           telefono=telefono,
                           email=email,
                           codice=codice,
                           pr=pr,
                           aut=aut,
                           marca=marca,
                           data_emi=data_emi,
                           data_scad=data_scad,
                           patente=patente,
                           nome_utente=nome_utente,
                           password_utente=password_utente,
                           generator=generator)

and the HTML:

div class="col col-6 border-top p-2">
                                <div class="row">
                                    <div class="col col-12">
                                        {{ form.hidden_tag() }} {{ form.nome_utente.label }} {{  form.nome_utente }}
                                    </div>
                                    <div class="col col-12 pt-3">
                                        <button type="button" onclick='{{ form.nome_utente.default }} = {{ generator.generate_user() }}'>Genera Nome Utente</button>
                                    </div>
                                </div>

                            </div>
                            <div class="col col-6 border-top p-2">
                                <div class="row">
                                    <div class="col col-12">
                                        {{ form.hidden_tag() }} {{ form.password_utente.label }} {{  form.password_utente }}
                                    </div>
                                    <div class="col col-12 pt-3">
                                        <button type="button" onclick='{{ generator.generate_pass() }}'>Genera Password</button>
                                    </div>

                                </div>
                            </div>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source