0
首先,aldyn_forms對我的用戶非常有用。現在我想爲Django CMS添加一個自定義「範圍」元素(<input type="range" />
)。我嘗試了以下,但它不呈現元素。我究竟做錯了什麼?創建自定義aldryn_forms插件
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)
謝謝!我也忘了把我的插件包裝成一個表格,因此它沒有呈現... –