5dollarwhitebox.org - theboxownsyou

  • blog
  • projects
  • articles
  • tech wiki
  • about
  • login
Home › Python Reference

RSS Feed

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.

‹ Python: Dual Output / Logging up Python: Getting TurboMail to work with TurboGears 2.0 ›
  • Printer-friendly version

Post new comment

The content of this field is kept private and will not be shown publicly.
Input format
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <diff>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <perl>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options



Who's online

There are currently 0 users and 1 guest online.
  • blog
  • projects
  • articles
  • tech wiki
  • about
  • login

5dollarwhitebox.org is not responsible in anyway for actions performed based on information found on this site.