If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Widgets

web2py provides a flexible method for applying various input and output controls to your form fields. Input controls are called widgets and output controls are called representations. Here is a sample widget taken directly from the book (7.5):

def my_string_widget(field, value):
    return INPUT(_name=field.name,
                 _id="s_s\" field._tablename, field.name),
                 _class=field.type,
                 _value=value,
                 requires=field.requires)

Field('comment', 'string', widget=my_string_widget)

This makes it very easy to customize your forms in a DRY manner. The application below includes 8 ready to use widgets. Here are screenshots of a few:

alt text

alt text

alt text

alt text

Download application and enjoy! All of the widget code (excluding any required javascript, css or images) is in A_widgets.py in the models folder.

Related slices

Comments (5)

  • Login to post



  • 0
    asmancom 14 years ago
    Again a great slice Nathan!!! Would be great if you could additionally implement the Progressbar Widget from the Client Tools here.

  • 0
    tom 14 years ago
    Hi, I was trying to find out how you managed to put few widgets on one page. I mean for example on the page with Cascading select widget (http://127.0.0.1:8000/widgets/default/cascading_select) there is also: Created on: - Date picker widget Rating: - Star rating widget Can you show me were and how do you define to display few widgets on the same page?

  • 0
    mrfreeze 14 years ago
    Each widget is applied to a field like so:
    db.table.field.widget = the_widget
    Is that what you mean?

  • 0
    tom 14 years ago
    I'm working on implementing your cascading_select widget on my custom registration form. This is how my code is ordered: in application\controllers\default.py
    def user():
        return dict(form=auth())
    
    in views\default\user.html
    {{if request.args(0)=='login':}}
    {{=form.custom.begin}}
    {{#table with login custom fields}}
    .
    .
    .
    {{=form.custom.submit}}
    {{=form.custom.end}}
    
    {{elif request.args(0)=='register':}}
    {{=form.custom.begin}}
    {{#table with registration custom fields (for example):}}
    {{=form.custom.label01}}{{=form.custom.widget01}}
    {{=form.custom.label02}}{{=form.custom.widget02}}
    {{=form.custom.label03}}{{=form.custom.widget03}}
    .
    .
    .
    {{=form.custom.submit}}
    {{=form.custom.end}}
    
    In the registration part I'd like to include your cascading_select widget. In my database I have 3 separate tables with state, city and zipcode. During registration I'd like to add id's of this 3 data (selected state, city, and zipcode) to user record. If I understand it correctly in my user_auth table I should have fields like state, city and zipcode. Can you tell me how should I modify my def user(): and registration form to have it working? Or maybe I need to modify something else yet? I'll appreciate your help. Best regards.

  • 0
    beefheart 11 years ago

    To get this working on newer versions of web2py add this to your model:

    response.generic_patterns = ['*'] if request.is_local else []

Hosting graciously provided by:
Python Anywhere