Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在wagtail的form builder中将自定义占位符添加到小部件中_Python 3.x_Django_Django Forms_Wagtail - Fatal编程技术网

Python 3.x 如何在wagtail的form builder中将自定义占位符添加到小部件中

Python 3.x 如何在wagtail的form builder中将自定义占位符添加到小部件中,python-3.x,django,django-forms,wagtail,Python 3.x,Django,Django Forms,Wagtail,我正在尝试向表单中的每个输入元素添加一个自定义占位符作为元素名 例如,如果元素名称为 姓 占位符应为: 占位符:姓氏 我这里的问题是如何从函数get\u form() 我试图通过从内部访问django formsself.field来获取每个元素的名称 这是我的表单模型: from django.db import models from modelcluster.fields import ParentalKey from wagtail.admin.edit_handlers import

我正在尝试向表单中的每个输入元素添加一个自定义占位符作为元素名

例如,如果元素名称为

占位符应为:

占位符:姓氏

我这里的问题是如何从函数get\u form()

我试图通过从内部访问django formsself.field来获取每个元素的名称

这是我的表单模型:

from django.db import models

from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import (
    FieldPanel,
    FieldRowPanel,
    InlinePanel,
    MultiFieldPanel
)
from wagtail.core.fields import RichTextField
from wagtail.contrib.forms.models import (
    AbstractEmailForm,
    AbstractFormField
)
from wagtail.images.edit_handlers import ImageChooserPanel
from django.forms import widgets


class FormField(AbstractFormField):
    page = ParentalKey(
        'ContactPage',
        on_delete=models.CASCADE,
        related_name='form_fields',
    )


class ContactPage(AbstractEmailForm):
    template = "contact/contact_us.html"
    # This is the default path.
    # If ignored, Wagtail adds _landing.html to your template name
    landing_page_template = "contact/contact_us_landing.html"

    banner_image = models.ForeignKey(
        'wagtailimages.Image',
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
        related_name='+',
    )
    description = models.TextField(blank=True, null=True)
    thank_you_text = RichTextField(blank=True)

    content_panels = AbstractEmailForm.content_panels + [
        ImageChooserPanel('banner_image'),
        FieldPanel('description'),
        InlinePanel('form_fields', label='Form Fields'),
        FieldPanel('thank_you_text'),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel("subject"),
        ], heading="Email Settings"),
    ]

    def get_form(self, *args, **kwargs):
        form = super().get_form(*args, **kwargs)
        # iterate through the fields in the generated form
        for name, field in form.fields.items():
            # here we want to adjust the widgets on each field
            if isinstance(field.widget, widgets.Select):
                field.widget.attrs.update({'class': 'nic-select'})
                # TODO: add place holder
            if isinstance(field.widget, widgets.Input):
                field.widget.attrs.update({'placeholder': ''})
                # TODO: add place holder
            if isinstance(field.widget, widgets.Textarea):
                field.widget.attrs.update({'placeholder': ''})
        return form