Python: DynamicLabel Form Field for Turbo Gears 2.0 ToscaWidgets
drks — Thu, 2009-12-31 00:51
Some time you might have an object attribute that you want to display dynamically as part of a form, but don't want to make it editable. For example, restricting the modification of a users 'user_name' being that changing it in the middle of a session causes havok. Well, I've created a simple form field called DymanicLabel for just that case:
yourapp/widgets/templates/dynamic_label.html:
<div xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
id="${id}"
class="${css_class}"
py:attrs="attrs">
${value}
</div>yourapp/widgets/forms.py:
class DynamicLabel(Label): value = '' template = 'yourapp.widgets.templates.dynamic_label'
Copy those in place, and then you'd setup your form widget to be something like:
from yourapp.widgets.forms import DynamicLabel class UserEditForm(TableForm): fields = [ # this is what we are replacing: # --- #TextField('user_name', label_text='User Name', # validator=UnicodeString(not_empty=True)), DynamicLabel('user_name', label_text="User Name", suppress_label=False), ]
Then in your template you might have something like:
<div py:replace="tmpl_context.form(
value=dict(user_id=user.user_id,
user_name=user.user_name,
email_address=user.email_address,
))">Input Form</div>The value of user.name will be displayed in your DynamicLabel because you gave it the variable name 'user_name'.
This is a shady post... I know... I'll try and clean it up and make it a proper article in the near future.
RSS Feed
Post new comment