Мне было интересно, как я могу переопределить функции render
и trigger_froala
в Django.Могу ли я создать свой собственный виджет, который переопределяет две функции?
from django.forms import widgets, Media
from django.utils.safestring import mark_safe
from django.conf import settings
import json
from . import PLUGINS, PLUGINS_WITH_CSS, THIRD_PARTY, THIRD_PARTY_WITH_CSS
try:
from django.urls import NoReverseMatch, reverse
except ImportError:
from django.core.urlresolvers import reverse, NoReverseMatch
class FroalaEditor(widgets.Textarea):
def __init__(self, *args, **kwargs):
self.options = kwargs.pop('options', {})
self.plugins = kwargs.pop('plugins', getattr(settings, 'FROALA_EDITOR_PLUGINS', PLUGINS))
self.third_party = kwargs.pop('third_party', getattr(settings, 'FROALA_EDITOR_THIRD_PARTY', THIRD_PARTY))
self.theme = kwargs.pop('theme', getattr(settings, 'FROALA_EDITOR_THEME', None))
self.include_jquery = kwargs.pop('include_jquery', getattr(settings, 'FROALA_INCLUDE_JQUERY', True))
self.image_upload = kwargs.pop('image_upload', True)
self.file_upload = kwargs.pop('file_upload', True)
self.language = (getattr(settings, 'FROALA_EDITOR_OPTIONS', {})).get('language', '')
super(FroalaEditor, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
html = super(FroalaEditor, self).render(name, value, attrs)
el_id = self.build_attrs(attrs).get('id')
html += self.trigger_froala(el_id, self.get_options())
return mark_safe(html)
def trigger_froala(self, el_id, options):
str = """
<script>
$(function(){
$('#%s').froalaEditor(%s)
});
</script>""" % (el_id, options)
return str