Hi, I would like to create a form having:
- a text field with some classroom ID, and
- a set of radio-buttons, one for each child in it.
This would work in steps:
- The form would open up with input 1 only, empty of course
- I would enter in it some class ID, and push a button
- The server would find how many student in that classroom, and what are their names, and present me a radio button set, from which I would choose one to do something, present his research next week, or just anything.
- I wold push a(second) button, and have the server send him/her an email, or anything else.
It seems to me a common enough approach, and yet I went through the Django documentation, and looked up the Internet, but apparently nobody else ever needed this…thank you for any help!
You first need to decide whether you want to do this as a set of related pages or as a single page.
Doing it as a set of related pages is the “pure Django” solution. Page 1 is the form with the text field and a submit button. That button submits the entry, and the server returns Page 2 with the radio buttons and a submit button. That second button then submits the selections to the view that is going to do whatever.
There’s no specific documentation for something like this because this is just a typical Django project.
Or, you can do it single page style, where the first button makes an AJAX-style call to a view to retrieve the information for the buttons and updates the page. (And there are many variations on exactly how that is to be done.)
However you choose to implement this, it will require some JavaScript. Django itself is fundamentally unable to update just a part of a page.
Actually, many people do. That’s why projects like HTMX exist - to make this type of interface very easy to implement.
Side note: If the classroom IDs are a defined set, I would strongly recommend that the selection of a classroom ID be a drop-down select widget and not a text entry box.
Thank you Ken. I have no problem with the two-page solution,
and I also think I know how to make the drop-down select.
Where I am stumped is with the variable-length radio-button list,
but actually now that I think of it, that could be a drop-down list too !?
I will try this way then. Thanks again!
That’s easy.
Assuming that the students are entries in some model, then the form field is a ModelChoiceField
, where the choices are defined by the classroom ID being selected.
You can then render this as either a select box (the default), or by using the RadioSelect
widget.