For the fundamentals of using forms in HTML, see the MDN forms tutorial
In particular, Sending and retrieving form data
Key things to keep in mind
- Each form control needs to be associated with a label for accessibility
- All named form elements send a value back to the server
- In the case of GET requests, values go into the URL as a query parameter
- In the case of search, we have controls that are not nested in a form element. We have to use the form attibute to link inputs to the parent form
Django form reference explains how forms work in django
- Forms objects are initialised from the submitted data
- Form objects can be bound (containing submitted data) or unbound
- Passing a bound object to the context, means you can preserve already entered values when rerendering the form after it is submitted
- We can completely ignore anything about model forms and formsets as we are not using those
MDN has a Django forms tutorial that is beginner friendly. Note this walks you through a POST form, rather than a GET form.
https://github.com/alphagov/finder-frontend/ has very similar functionality, but is written in Rails
- server side rendered search pages
- includes filtering
- gets search results via an API
- main view https://github.com/alphagov/finder-frontend/blob/main/app/controllers/finders_controller.rb
- main template https://github.com/alphagov/finder-frontend/blob/main/app/views/finders/show.html.erb
- they extracted some helper objects from the view, like ResultSetPresenter, ResultSetParser, PaginationPresenter FacetTagsPresenter example tests for the above: https://gist.github.com/MatMoore/0a7aaf88de2d283b42fc71e85d07d126