text: from django.forms import widgets
  from django.utils.safestring import mark_safe
  from django.core.urlresolvers import reverse
  import json
  from django.conf import settings
  
  
  GLOBAL_OPTIONS = getattr(settings, 'REDACTOR_OPTIONS', {})
  
  INIT_JS = """<script type="text/javascript">
    jQuery(document).ready(function(){
      $("#%s").redactor(%s);
    });
  </script>
  """
  
  
  class RedactorEditor(widgets.Textarea):
  
      class Media:
          js = (
              'redactor/jquery.min.js',
              'redactor/jquery-migrate.min.js',
              'redactor/redactor.min.js',
          )
          css = {
              'all': (
                  'redactor/css/redactor.css',
                  'redactor/css/django_admin.css',
              )
          }
  
      def __init__(self, *args, **kwargs):
          self.upload_to = kwargs.pop('upload_to', '')
          self.custom_options = kwargs.pop('redactor_options', {})
          self.allow_file_upload = kwargs.pop('allow_file_upload', True)
          self.allow_image_upload = kwargs.pop('allow_image_upload', True)
          super(RedactorEditor, self).__init__(*args, **kwargs)
  
      def get_options(self):
          options = GLOBAL_OPTIONS.copy()
          options.update(self.custom_options)
          if self.allow_file_upload:
              options['fileUpload'] = reverse(
                  'redactor_upload_file',
                  kwargs={'upload_to': self.upload_to}
              )
          if self.allow_image_upload:
              options['imageUpload'] = reverse(
                  'redactor_upload_image',
                  kwargs={'upload_to': self.upload_to}
              )
          return json.dumps(options)
  
      def render(self, name, value, attrs=None):
          html = super(RedactorEditor, self).render(name, value, attrs)
          final_attrs = self.build_attrs(attrs)
          id_ = final_attrs.get('id')
          html += INIT_JS % (id_, self.get_options())
          return mark_safe(html)
  