Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

In our previous recipe, we created CategoryField. This field used the Select widget, which was provided by the Select superclass. Let's replace the Select widget with a radio input in models.py:

from wtforms.widgets import html_params, Select, HTMLString 
 
class CustomCategoryInput(Select): 
 
    def __call__(self, field, **kwargs): 
        kwargs.setdefault('id', field.id) 
        html = [] 
        for val, label, selected in field.iter_choices(): 
            html.append( 
                '<input type="radio" %s> %s' % ( 
                    html_params( 
                        name=field.name, value=val,                            checked=selected, **kwargs 
                    ), label 
                ) 
            ) 
        return HTMLString(' '.join(html)) 
 
class CategoryField(SelectField): 
    widget = CustomCategoryInput() 

    # Rest of the code remains same as in last recipe Creating  custom field and validation ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content