= A simple example = Here's a simple example of how we could use Albatross Forms to collect a username and password from the user. We need to define a model class to hold the data: {{{#!python import pwd, crypt class User: def __init__(self, username, password): self.username = username self.password = password def is_password_valid(self): try: pw = pwd.getpwnam(self.username) except KeyError: return False return (crypt.crypt(self.password, pw.pw_passwd) == pw.pw_passwd) }}} Next, we need to define a form to display the fields: {{{#!python from albatross.ext.form import * class LoginForm(FieldsetForm): def __init__(self, user): fields = ( TextField('Username', 'username'), PasswordField('Password', 'password'), ) fieldsets = (Fieldset(fields), ) buttons = Buttons(( Button('Login', 'login'), )) FieldsetForm.__init__(self, 'User login', fieldsets, buttons=buttons) self.load(user) }}} We need to create an instance of the Login model and maintain that so that any captured data is retained. In our {{{login.py}}}, we use: {{{#!python def page_enter(ctx): if not ctx.has_value('user'): ctx.locals.user = User('', '') ctx.add_session_vars('user') ctx.locals.login_form = LoginForm(ctx.locals.user) ctx.add_session_vars('login_form') ctx.locals.login_error = '' }}} In {{{login.html}}}, to display the form to the user we use: {{{#!html }}} When the user presses the "Login" button, it will come back to our {{{page_process}}} method in {{{login.py}}}. We check if the username and password are correct and punt them into the application proper (via the "search" page) or tell them they've got it wrong. {{{#!python def page_process(ctx): if ctx.req_equals('login'): # nothing to validate ctx.locals.login_form.merge(ctx.locals.user) if ctx.locals.user.is_password_valid(): ctx.redirect('search') else: ctx.locals.login_error = 'Login incorrect' }}}