Login

ShowOnly widget for froms

Author:
alexmeisel
Posted:
November 13, 2008
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 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.