I’m back with another Django mini tutorial! This is something that probably a lot of people already know how to do, but since there’s no official documentation for creating custom Django form field widgets, I thought I’d write a post about the information I pieced together in my research. Most of what I learned about creating these widgets came from inspecting GitHub repositories from others who have done this before – so I’m no expert and I’m making a lot of assumptions. Nevertheless, something here might be useful to someone else!
For the project I’m working on I wanted to have a form field to write a short essay for an application. The essay could have a minimum word count, a maximum word count, both or neither. In the help text for the form field I displayed the essay length requirements, but I also wanted to display a word count and indicate if the current length was within the length limits. The resulting control I created looked something like this:
In this mini tutorial I’m going to walk through the steps that I used to create this custom widget.
Continue reading “Django example: creating a custom form field widget”