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')
Comments
I may be missing something, but how does the form pass back the value for the field? Even in conjunction with 1184.
#
Uploaded the wrong code ... Sorry about that, here is the correct version.
#
FWIW a slight modification which shows a different text (useful for lookup values), while retaining the field value in the hidden field. Used with http://www.djangosnippets.org/snippets/916/ it can be dynamically changed if the form instance is initiated with a model instance.
Pretty basic stuff, but it took a while to figure out what was accessible where.
class ShowOnly(Input): """ Show only the data do NOT have a input field """ input_type = 'hidden'
#