创建自定义 aldryn_forms 插件
Create a custom aldryn_forms plugin
首先,aldyn_forms 对我的用户来说很棒。现在我想为 Django CMS (<input type="range" />
) 添加自定义 "range" 元素。我尝试了以下方法,但它不呈现元素。我做错了什么?
cms_plugins.py
:
from aldryn_forms.cms_plugins import Field
from cms.plugin_pool import plugin_pool
from django.forms import Widget
from django.utils.translation import ugettext_lazy as _
from django import forms
class RangeWidget(Widget):
def __init__(self, attrs=None):
super().__init__(attrs)
def render(self, name, value, attrs=None):
return 'This should render, but it does not render'
class RangeForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
if 'instance' not in kwargs: # creating new one
initial = kwargs.pop('initial', {})
initial['required'] = False
kwargs['initial'] = initial
super().__init__(*args, **kwargs)
class Meta:
fields = ['label', 'help_text', 'required', 'required_message', 'custom_classes', 'min_value', 'max_value']
class RangeElement(Field):
widget = RangeWidget
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def widget_attrs(self, widget):
return super().widget_attrs(widget)
class RangeField(Field):
name = _('Range Field')
form = RangeForm
form_field = RangeElement
form_field_widget = RangeElement.widget
fieldset_general_fields = [
'label',
('min_value', 'max_value'),
'required',
]
fieldset_advanced_fields = [
'help_text',
'required_message',
'custom_classes',
]
plugin_pool.register_plugin(RangeField)
插件 form_field
和 form_field_widget
应该指向 django 表单字段和表单字段小部件。
您可以在 https://github.com/aldryn/aldryn-forms/blob/master/aldryn_forms/cms_plugins.py#L740
中查看示例
所以RangeElement
应该继承自django表单字段class而不是aldryn-forms
Field
class.
首先,aldyn_forms 对我的用户来说很棒。现在我想为 Django CMS (<input type="range" />
) 添加自定义 "range" 元素。我尝试了以下方法,但它不呈现元素。我做错了什么?
cms_plugins.py
:
from aldryn_forms.cms_plugins import Field
from cms.plugin_pool import plugin_pool
from django.forms import Widget
from django.utils.translation import ugettext_lazy as _
from django import forms
class RangeWidget(Widget):
def __init__(self, attrs=None):
super().__init__(attrs)
def render(self, name, value, attrs=None):
return 'This should render, but it does not render'
class RangeForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
if 'instance' not in kwargs: # creating new one
initial = kwargs.pop('initial', {})
initial['required'] = False
kwargs['initial'] = initial
super().__init__(*args, **kwargs)
class Meta:
fields = ['label', 'help_text', 'required', 'required_message', 'custom_classes', 'min_value', 'max_value']
class RangeElement(Field):
widget = RangeWidget
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def widget_attrs(self, widget):
return super().widget_attrs(widget)
class RangeField(Field):
name = _('Range Field')
form = RangeForm
form_field = RangeElement
form_field_widget = RangeElement.widget
fieldset_general_fields = [
'label',
('min_value', 'max_value'),
'required',
]
fieldset_advanced_fields = [
'help_text',
'required_message',
'custom_classes',
]
plugin_pool.register_plugin(RangeField)
插件 form_field
和 form_field_widget
应该指向 django 表单字段和表单字段小部件。
您可以在 https://github.com/aldryn/aldryn-forms/blob/master/aldryn_forms/cms_plugins.py#L740
中查看示例所以RangeElement
应该继承自django表单字段class而不是aldryn-forms
Field
class.