Components for web2py HTML pages NOT derived from layout.html

I wrote a post to the google web2py-users group on this subject. This WordPress post will be my log of the work i am doing on this subject.

Here is a copy of my original web2py-users group post.

Dear web2py Forum,

Often i find myself making View pages that are NOT derived from layout.html and yet wanting to include some aspects of its capabilities.

I thought the web2py “Component” concept might help to deliver a modular approach.

My initial thoughts would be to pair small CSS files and HTML Component definitions for the following:

  • menu
  • login/register
  • left sidebar
  • right sidebar
  • footer
  • ajax
  • flash response
  • mobile
  • responsive

while maintaining a list of dependencies among them.

Has anyone thought about this granular approach before?

I would not want to use a Wizard for this, because in my experience, the generated code is unfamiliar to me and difficult to customize.

Thanks for a GREAT framework.

Love and peace,


1. i looked up web2py component

By def it seems that components

communicate with the component controller function via Ajax

Therefore the list of dependencies must contain ajax stuff.

2. add a new component to list:

navbar – is a complex component containing both menu and auth login stuff


#components, #css, #gui, #html-helpers, #idioms, #web2py

Web2py URL Mapping

URL Mapping from the Book Chapter 4 – Dispatching. These are the basics there’s more detail in the book.

The URL    maps to:

The function f() in controller “” in application “a“.

  • If f is not present, web2py defaults to the index controller function.
  • If c is not present, web2py defaults to the controller.
  • If a is not present, web2py defaults to the init application.
  • If there is no init application, web2py tries to run the welcome application.
  • The extension .html is optional.
  • The extension .html is assumed as default.
  • The extension determines the extension of the view that renders the output of the controller function f().
  • The extension allows the same content to be served in multiple formats (html, xml, json, rss, etc.).

Web2py maps GET/POST requests of the form:

As before, to function f in controller “” in application a

and it stores the URL parameters in the requestvariable as follows:

    request.args = ['x', 'y', 'z']
    request.vars = {'p':1, 'q':2}
    request.application = 'a'
    request.controller = 'c'
    request.function = 'f'

    request.url = url of request
    request.ajax = False  #by default
    if request.ajax == True and wasInitiatedByAWeb2pyComponent:
        request.cid = componentName

#python, #web2py

Web2py DAL Quick Reference

The following was referred to here in a post by Iceberg on May 4, 2009 at 9:14 AM on the web2py-users google group. It is a merged version of the old and new DAL info.

I couldn’t find it published anywhere so I am publishing it here so I can refer to it. Some of it is here.

web2pyTM Database Abstraction Layer (DAL)

Quick Reference

define_table, insert, count, delete, update

db = SQLDB('postgres://user:password@hostname/db', pools=10)
db.define_table('person',db.<a href="">Field</a>('name','string'))
id= db.person.insert(name='max')
rows = db(query).select(
for row in rows: print

Examples of uri strings for SQLDB


Valid field Types

db.Field(name, 'string')
db.Field(name, 'text')
db.Field(name, 'password')
db.Field(name, 'blob')
db.Field(name, 'upload')
db.Field(name, 'boolean')
db.Field(name, 'integer')
db.Field(name, 'double')
db.Field(name, 'time')
db.Field(name, 'date')
db.Field(name, 'datetime')
db.Field(name, db.referenced_table) # reference field

Valid Field Attributes

length (only for string type, defaults to 32)
default (defaults to None)
required (defaults to False)
notnull (defaults to False)
requires (<a href="">validator or list of validators</a>, for forms)
comment (for forms)
widget (for forms)
represent (for forms)
readable (for forms)
writable (for forms)
update (default value if the record is updated)
uploadfield (for upload fields)
authorize (for upload fields, function to be used if data can be downloaded, see authentication)
autodelete (for upload fields, if set to true linked uploaded images are removed upon deletion of the record)
label (for forms)

Changing the list of fields or field types in a model, triggers an automatic migration, i.e. web2py generates SQL to alter the table accordingly. If the table does not exist it is created. Migration actions are logged in the file sql.log accessibled via the admin/design interface. Migration can be turned off on a per-table basis by passing migrate=False to define_table.
Select Attributes

rows = db(query).select(*fields, orderby=..., left=..., groupby=..., having=..., limitby=..., cache=...)

DAL Shortcuts

db['person']                   ### db.person
db.person['name']              ###
db['person']['name']           ###
db.person[0]=dict(name='Max')  ### insert
db.person[id]=dict(name='Max') ### update by
print db.person[id]            ### select by
del db.person[id]              ### delete by</pre>

Truncate and Drop a Table


**Reference Fields and Inner Joins*

db.define_table('dog',db.<a href="">Field</a>('name'))
db.define_table('friendship', db.<a href="">Field</a>('person',db.person), db.<a href="">Field</a>('dog',
rows = db(friends).select(,
for row in rows: print, 'is friend of',

Left Outer Joins

rows = db(query).select(,,
for row in rows: print, 'is friend of', or 'nobody'

Complex Queries

query = (|(('Max'))
query = (
query ='m%')
query ='max','Max','MAX'))
query = db.person.birth.year()+1==2008
rows = db(query).select()

Nested Selects

query =


''' Notes:
Aggregate functions: 
    db.table.field. sum()




''' Notes:
cache=(model,cache_timeout) where model can be:
    cache.memcache or 
    user defined caching model
cache_timeout is in seconds.

CSV Input/Output

# CVS Input
# CVS Output

HTML Output

print rows.xml()

Set Field Validators,

Generate and Process a Form from a Model

form = SQLFORM(db.friendship)
if form.accepts(request.vars, session): response.flash='record inserted'
elif form.errors: response.flash='form errors'
''' Note:
The form can then be displayed in a view with:

SQL + HTML Examples

# Given:
# Then:
SQLFORM (rows) # turns the rows into a CSS friendly table
SQLFORM(db.users)  # makes an input form for users
SQLFORM(db.users,rows[i]) # makes an edit form for the user in rows[i]
SQLFORM(db.users,rows[i],deletable=True) # makes an edit/delete form for the user in row[i]
SQLFORM(....,fields=['name','email']) # allows defining which fields should be displayed in the form. Only those fields will be validated.
SQLFORM(....,labels=['name':'Your Name']) # allows changing the labels of the listed fields.

''' Notes:
A form=SQLFORM(…) object has one method of practical interest:
The above method: 
    1. Processes the input variables (in request.vars) in the session
    2. Returns true if the form is valid, false otherwise. 
- Processed variables are in     form.vars
- Errors are in                  form.errors. 
- The form is modified accordingly. 
- If the form is accepted: 
    accept also performs the appropriate insert/update/delete in the database.

Extended Usage
The DAL API is automatically exposed in web2py 
    controllers and 
but you can access them anywhere with:
from gluon.sql import * 

Doctest Examples
These are old syntax but good examples. They use:

  • SQLDB instead of DAL and
  • SQLField instead of Field

However I believe they are still useful.

>>> db=SQLDB("sqlite://test.db")
>>> #OR db=SQLDB("mysql://username:password@host:port/dbname")
>>> #OR db=SQLDB("postgres://username:password@host:port/dbname")

# syntax: SQLField('fieldname','fieldtype',length=32,
#                   required=False, default=None,
#                   requires=[IS_EMAIL(error_message='invalid email')])

>>> tmp=db.define_table('users',
      # But notice that SQLField(...) is depreciated and you'd better use db.Field(...) instead

# Insert a field

>>> db.users.insert(stringfield='a',booleanfield=True,
                   uploadfield=None, integerfield=5,doublefield=3.14,

# Drop the table

>>> db.users.drop()

# Examples of insert, select, update, delete

>>> tmp=db.define_table('person',
>>> person_id=db.person.insert(name="Marco",birth='2005-06-22')
>>> person_id=db.person.insert(name="Massimo",birth='1971-12-21')
>>> len(db().select(db.person.ALL))
>>> me=db([0] # test select
>>> db('Massimo').update(name='massimo') # test update
>>> db('Marco').delete() # test delete

# Update a single record

>>> me.update_record(name="Max")

# Examples of complex search conditions

>>> len(db(('Max')&(db.person.birth>> len(db(('Max')|(db.person.birth>> me=db([0]

# Examples of search conditions using extract from date/datetime/time

>>> len(db(db.person.birth.month()==12).select())
>>> len(db(db.person.birth.year()>1900).select())

# Example of usage of NULL

>>> len(db(db.person.birth==None).select()) ### test NULL
>>> len(db(db.person.birth!=None).select()) ### test NULL

# Examples of search conditions using lower, upper, and like

>>> len(db('MAX').select())
>>> len(db('%ax')).select())
>>> len(db('%AX')).select())
>>> len(db('%AX')).select())

# orderby, groupby and limitby

>>> people=db().select(,
>>> people=db().select(,orderby=order)
>>> people=db().select(,orderby=order,
>>> people=db().select(,orderby=order,limitby=(0,100))

# Example of one 2 many relation

>>> tmp=db.define_table('dog',

# A simple JOIN

>>> len(db(

# Drop tables

>>> db.person.drop()

# Example of many 2 many relation and SQLSet

>>> tmp=db.define_table('author',SQLField('name'),
>>> tmp=db.define_table('paper',SQLField('title'),
>>> tmp=db.define_table('authorship',
>>> pid=db.paper.insert(title='QCD')
>>> tmp=db.authorship.insert(author_id=aid,paper_id=pid)

# Define a SQLSet

>>> authored_papers=db((
>>> for row in rows: print, row.paper.title
Massimo QCD

# Example of search condition using  belongs

>>> set=(1,2,3)
>>> rows=db(
>>> print rows[0].title

# Example of search condition using nested select

>>> nested_select=db()._select(db.authorship.paper_id)
>>> rows=db(
>>> print rows[0].title

# Output in csv

>>> str(,db.paper.title)),paper.title

# Delete all leftover tables

>>> db.authorship.drop()
>>> db.paper.drop()

# Commit or rollback your work

>>> db.commit() # or db.rollback()

''' Notes:
migrate can be:
    False (do not create/alter tables), 
    True (create/alter tables) or 
    a filename (create/alter tables and store migration information in the file).

There are little idiosyncrasies in every  backend like the fact that:
    "user" is not a valid field name in PostgreSQL, or 
    that sqlite3 will ignore the type of a field and allow you to put anything in it despite the declared type.
Each database has its own keywords that may conflict with your tablenames.

#dal, #web2py

Making my web2py site use a default_application other than welcome on WebFaction.

Here are the steps on webfaction.

  1. $ cd ~/your_path_to_web2py/web2py
  2. $ cp #there is no at first
  3. [edit as follows
        line was: default_application = ‘init’    # ordinarily set in base
        line now: default_application = ‘other’   # ordinarily set in base
  4. $ cd ../apache2/bin
  5. $ ./restart

#app, #default, #web2py

Fixing my "my web2py is unable to create a new application on WebFaction" problem.

Please go here to read a full discussion of the problem. Here is a summary of the steps i took to fix this problem.

  1. Made sure that memory capacity is not a problem.
    1. SSHed into my WebFaction account.
    2. $ ps -u user -o pid,rss,command
      output is: PID, Total memory in bytes, full path of process.
    3. I saw that i was over my memory limit.
    4. Upgraded to get more memory.
    5. Tested to see if that worked. IT DID NOT.
  2. I read this thread.
    1. Made sure that the web2py directory contained a directory named “deposit”.
    2. It did.
    3. Re-tested – NO GOOD.
  3. Opened a WebFaction Ticket.
    1. WebFaction Support suggested that I “…try running the web2py daemon script manually (“python2.5 web2py[.py]”) to initialize those systems”.
    2. Decided i would do that.
  4. Ran web2py from the SSH command line to initialize it.
    1. $ cd ~/path_to_troubled_web2py_instance/web2py/
    2. $ python2.5
      Up came web2py saying:
      web2py Enterprise Web Framework
      Created by Massimo Di Pierro, Copyright 2007-2011
      Version 1.92.1 (2011-02-16 15:04:40)
      Database drivers available: SQLite3, pymysql, PostgreSQL
      Starting hardcron…
      choose a password:
      please visit:
      use “kill -SIGTERM 6744” to shutdown the web2py server
    3. I let it run for a couple of minutes to initialize.
    4. $ kill -SIGTERM 6744  # The number 6744 varies. Use the number mentioned by web2py.
    5. Re-tested by by using IE8 to https login to the associated web2py admin from my windows XP desktop. SUCCESS. I was able to create the “home” app.  


Concatenating web2py HTML Helpers

Say you want to produce something like this list of links dynamically, into an HTML page, from a web2py controller:

Here is what you would put into your controller to concatenate (append) HTML Helpers:
def index():
    page_info = DIV(
                A(‘NOAA Weather’, _href=’’), BR(),
                A(‘Google Mobile’, _href=’’), BR(),
                A(‘Twitter Mobile’, _href=’’)
    return dict(page_info=page_info)
Here is what would go into your associated view:
{{extend ‘layout.html’}}
Here’s the reason. Lot’s of HTML helpers are derived from the DIV helper. According to the web2py epydocs at line 523, DIV
"Behaves like a dictionary regarding updating of attributes. 
Behaves like a list regarding inserting/appending components."

#html-helpers, #web2py