HTMX in selection fields in a py4web FORM

47 views
Skip to first unread message

domel zetaz

unread,
Jun 11, 2026, 1:38:11 PMJun 11
to py4web
The field requires a `name` attribute with a value, otherwise, the AJAX action associated with the field's `onchange` event will not send the selected value to the invoked procedure. The generated form does not include this attribute. 
Adding the missing attribute to `attrs` allowed HTMX to locate and send it.

Massimo DiPierro

unread,
Jun 21, 2026, 9:21:47 PM (13 days ago) Jun 21
to py4web
All fields should have a name, except the submit button. I added it now. If you still experience this problem, please send an example of the form so we can debug

domel zetaz

unread,
Jun 22, 2026, 5:35:09 PM (12 days ago) Jun 22
to py4web
The problem is with a field in the Form, not the submit button, when using AJAX.
I used the changes you made on GitHub, but the difference was adding the name to the submit button, not at the field level, as required.
In my case, the "plan" field when a change is made, should be routed through its "onchange" event to a procedure that receives the value of that field.
I'm sending the script and template for debugging.
In the field's helper attributes, I commented out the "_name" attribute, and when running the script, you can see the result in the "pruajax" action... neither the name of the "plan" field nor its value is sent.
Removing the comment from the attribute fixes it.


CONTEXT:
   Windows: 10
   Python: 3.12.10 32-bits
   Py4Web: 1.20260520.0

SCRIPT:
titulo = 'Planes'
@action("formconajax")
@preferred('basicos/form_titulo.html')
def formconajax():
   form = Form([Field('plan', type='integer', length=1, label='Plan',
                              requires=IS_IN_SET([2, 3, 5, 7], zero=None)),
               ], dbio=False)
   # Obtener helper del campo plan
   campo_helper = form.custom.widgets['plan']
   # asignar atributos al campo plan
   campo_helper.attributes = {
   #                           "_name":"plan",
                              "_autofocus":True,
                              "_hx-post": URL("pruajax"),
                              "_hx-trigger":"change",
                              "_hx-swap":"none",
                             }
   return {'form':form, 'titulo':titulo}

#-----
# acción ajax
#-----
@action("pruajax")
@preferred()
def pruajax():
   print(request.POST)

TEMPLATE:  form_titulo.html
[[extend 'layout_super.html']]

[[block center]]
    [[=H6(titulo)]]
    [[=form]]
[[end]]

P.S.: I'd like to thank you, Massimo, and the entire team that created and supports Py4Web. I've been working with web2py for almost 10 years now, with great success.
Regards from Montevideo, Uruguay.
Reply all
Reply to author
Forward
0 new messages