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

This is a simple web2py todo application in which the index view pulls in a showtasks.load view file to display a populated grid.

 

Executive summary:

  • Split the controller into two methods. In this case, index() is supplemented by showtasks(), even though the showtasks() isn’t called directly.
  • Split the view up into two files. In this case index.html is separated from showtasks.load
  • Use the LOAD helper in the calling view (index.html in this case, but it could be any view) to insert a view file named with an extension of .load instead of .html

 

Append this to db.py:

db.define_table('task',
    Field('title',unique=True,notnull=True),
    Field('description','text'),
    Field('priority','integer',default=3,
        requires=IS_IN_SET([1,2,3,4,5],
        labels=[T('Very low'), 
                T('Low'), 
                T('Medium'), 
                T('High'), 
                T('Very High')],
        zero=None)),
    Field('completed','boolean',default=False))
 

Add these controller methods to default.py:

# Use layout.html to create the "master" page.
# View will call view file showtasks.load.
def index():
    return locals()

# index.html view uses this
def showtasks():
    # Create a grid control.
    # Supports adding, deleting, 
    # editing, and searching records.
    return dict(
        grid=SQLFORM.grid(
            db.task,user_signature=False))

Replace /views/default/index.html with this:

{{extend 'layout.html'}}
{{=LOAD('default', 'showtasks.load',ajax=True)}}

Create the /views/default/showtasks.load:

{{if 'message' in globals():}}
<h3>{{=message}}</h3>
{{pass}}
{{=grid}}

 

Now run the app. It looks like any other web2py app but saves page load time by loading only the grid when needed, instead of refreshing the whole page.

 

 

 

Related slices

Comments (1)


Hosting graciously provided by:
Python Anywhere