On Sun, Aug 30, 2009 at 1:53 AM, Mark Anderson<nosrednak...@gmail.com> wrote:
> Hello,
>
> I wanted a field that would render in a rich text editor and store the data
> in a TextField so I created a field type of HtmlField and custom HtmlWidge.
> It works but I was wondering is anyone would be willing to give me feedback
> on best practices etc, This is my first attempt at subclassing and any
> points on would be great.  I would like modelform to set the class to rte
> and adjust rows/cols without me having to specify a widget.
>
> Thank you,
> Mark
>
> class HtmlWidget(Textarea):
>     def __init__(self, attrs=None):
>         super(HtmlWidget, self).__init__(attrs)
>
>     def render(self, name, value, attrs=None):
>         if value is None: value = ''
>         value = smart_unicode(value)
>
>         return mark_safe(\
>         u'<textarea name="%s" rows="12" cols="86" class="rte">%s</textarea>'
> % \
>               (name, escape(value)))
>
> class HtmlField(models.TextField):
>
>     def get_internal_type(self):
>         return "HtmlField"
>
>     def formfield(self, **kwargs):
>         kwargs['widget'] = HtmlWidget
>         return super(HtmlField, self).formfield(**kwargs)
>
>     def __init__(self, *args, **kwargs):
>         super(HtmlField, self).__init__(*args, **kwargs)
>
>
> >
>

The biggest thing is there is no need to rewrite the __init__ method
if you aren't changing anything in there.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to