创建自定义aldryn_forms插件

2021-05-16 09:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,aldyn_表单对我的用户非常好。现在我想为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)