pydanny / django-wysiwyg

A Django application for making Django textareas rich text editors. Certainly as a template tag and possibly as a form widget.
http://django-wysiwyg.readthedocs.org
MIT License
465 stars 63 forks source link

Create HTML Form field and model field #45

Open troygrosfield opened 9 years ago

troygrosfield commented 9 years ago

The code is rather simple:

HTMLFormField

class HTMLFormField(forms.CharField):
    """An html form field that handles the processing of html content from
    forms."""

    def to_python(self, value):
        if value:
            value = sanitize_html(clean_html(value))
        return super(HTMLFormField, self).to_python(value)

HTMLModelField

class HTMLField(with_metaclass(models.SubfieldBase, models.TextField)):
    """Model field that stores html."""

    def formfield(self, **kwargs):
        defaults = {'form_class': HTMLFormField}
        defaults.update(kwargs)
        return super(HTMLField, self).formfield(**defaults)

This code isn't fully tested, but I think that's all it would take.

vdboor commented 9 years ago

That's good pragmatic thinking. :+1: If we'd also add a CSS class to the output, the JavaScript code could automatically change the fields into a WYSIWYG area.