如何覆盖 Django 小部件功能(Froala 编辑器)?
How to override Django widget functions (Froala Editor)?
我想知道如何覆盖 Django 中的函数 render
和 trigger_froala
。我可以创建自己的小部件来覆盖这两个功能吗?
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
我知道怎么做了:
from django import forms
from django.utils.safestring import mark_safe
from froala_editor.widgets import FroalaEditor
class CustomFroalaEditor(FroalaEditor):
def __init__(self, *args, **kwargs):
super(CustomFroalaEditor, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
html = super(FroalaEditor, self).render(name, value, attrs)
return mark_safe(html)
我想知道如何覆盖 Django 中的函数 render
和 trigger_froala
。我可以创建自己的小部件来覆盖这两个功能吗?
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
我知道怎么做了:
from django import forms
from django.utils.safestring import mark_safe
from froala_editor.widgets import FroalaEditor
class CustomFroalaEditor(FroalaEditor):
def __init__(self, *args, **kwargs):
super(CustomFroalaEditor, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
html = super(FroalaEditor, self).render(name, value, attrs)
return mark_safe(html)