> I want to be able to catch the
> exception, flash and error message, and re-display the entry form
> still filled in from their previous input.
Something like this works for me:
class ContactForm(TableForm):
fields = [TextField('contact_name',label='User ID',
help_text='(enter User ID of the user)',
validator=validators.UnicodeString(not_empty=True, strip=True),
attrs={'size':30}),
RadioButtonList("contact_type", label='Contact type',
options=[('F', "Favorite"),
('B', "Blocked")],
default='F',
validator=validators.NotEmpty()),
SubmitButton(name='add', label='Add'),
SubmitButton(name='cancel', label='Cancel')]
contact_form = ContactForm()
class ContactController(controllers.Controller):
@expose(template="...")
@identity.require(identity.not_anonymous())
def add(self, tg_errors=None, tg_exceptions=None):
if tg_errors:
flash("Error: " + str(tg_errors))
if tg_exceptions:
flash("Exception: " + str(tg_exceptions))
value = {'contact_name' : '',
'contact_type' : 'F'}
return dict(form=contact_form,
form_title='Add a user to your favorite or blocked
list',
form_text="",
action='/contact/insert',
value=value)
@expose()
@validate(form=contact_form)
@error_handler(add)
@exception_handler(add)
@identity.require(identity.not_anonymous())
def insert(self, **data):
cuser = identity.current.user
user = User.get_by(user_name=data['contact_name'])
cuser.contacts.append(Contact(contact=user,
contact_type=data['contact_type']))
flash("Successfully added a contact")
raise redirect('/contact/contacts')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---