Creating a common forms set

An application can have loads of forms, depending on the design and purpose. Many of these forms will have common fields with common validators. Many of us might think, "Why not have common forms parts and then reuse them as and when needed?" This is very much possible with the class structure for forms' definition provided by WTForms.

How to do it…

In our catalog application, we can have two forms, one each for the Product and Category models. These forms will have a common field called Name. We can create a common form for this field, and then, the separate forms for the Product and Category models can use this form instead of having a Name field in each of them. This can be done as follows:

class NameForm(Form): name ...

Get Flask Framework Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.