Carl Karsten wrote on 02/01/08 01:34:
> newforms is great for creating forms.  I need something similar to generate a 
> display form from a model.
> 
> I looked at subclassing form, but what I would need to override is in the 
> widgets.  which is probably why my hope isn't a good one.  but for what I 
> need, 
> I only am using char fields, so I don't have to worry about widgets.
> 
> So, is there some way to render a form as just text?
> 

I've been using the following for something similar:

class PlainText(forms.HiddenInput):
     def render(self, name, value, attrs=None):
         return u'%s%s' % (forms.HiddenInput.render(self, name, value, 
attrs), value)

def myobject_formfield_callback(field, **kwargs):
     if field.name in ('hostname', 'ipaddress'):
         kwargs['widget'] = PlainText()
         return field.formfield(**kwargs)
     else:
         return field.formfield(**kwargs)


def myview(request):
     myobject = get_object_from_somewhere()
     initial = myobject.__dict__
     FormClass = forms.form_for_instance(myobject, 
formfield_callback=myobject_formfield_callback)
     form = FormClass(initial=initial)
     ...


In my example I just want certain fields to show as text. If you want 
that for all of them change the formfield_callback to always change the 
widget.

If you only want the plain value without a hidden field change the 
PlainText.render method accordingly.

Works for me.

hth
Steven

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to