Login

ShowOnly widget for froms

Author:
alexmeisel
Posted:
November 13, 2008
Language:
Python
Version:
1.0
Tags:
newforms forms display content
Score:
2 (after 2 ratings)

This form widget allows you to just display data in a rendered form, not giving the user the opportunity to change it. The initial data will just be carried through the form and showed to the user.

In combination with snipped 1184 you can make this even tamper safe. ;-)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class ShowOnly(forms.Widget):
    """
    Show only the data do NOT have a input field
    """
    input_type = 'hidden'

    def render(self, name, value, attrs=None):
        from django.utils.safestring import mark_safe
        from django.utils.encoding import force_unicode
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            value = force_unicode(value)
            final_attrs['value'] = force_unicode(value)
        return mark_safe(u'<input%s />%s' % (forms.util.flatatt(final_attrs),value))


# Example usage:
class SomeForm(forms.Form):
    justShowThis = forms.CharField(required = False, widget = ShowOnly(), initial = 'Input (not editable)', label = 'My nice Label')

More like this

  1. DisableableSelectWidget by notanumber 2 years, 10 months ago
  2. tag: render form field by crucialfelix2 5 years, 3 months ago
  3. Submit button as a form field by roxeteer 4 years, 1 month ago
  4. ManyToManyField no syncdb by powerfox 6 years, 1 month ago
  5. Multiple models display form by lbolognini 7 years, 7 months ago

Comments

brooks_lt (on November 13, 2008):

I may be missing something, but how does the form pass back the value for the field? Even in conjunction with 1184.

#

alexmeisel (on January 1, 2009):

Uploaded the wrong code ... Sorry about that, here is the correct version.

#

Please login first before commenting.