- 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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
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.
#
Please login first before commenting.