简体   繁体   中英

NamedImageFieldWidget does not render the image

I have a z3c form like this, that is not a dexterity content type :

from zope import schema
from plone.directives import form
from bsw.adel import personne
from plone.namedfile.field import NamedImage
from plone import namedfile
from plone.formwidget.namedfile import NamedImageFieldWidget

def get_portrait():
    login = api.user.get_current().id
    adel_personne = personne.Personne()
    ma_personne = adel_personne.get(id_personne=None,
                                    login=login)
    if ma_personne is not None:
        data = adel_personne.get_picture(id_personne=ma_personne.idPersonne,
                                         photo_filename=None,
                                     mode='v'
                                     )
        return namedfile.NamedImage(data=base64.b64decode(data), filename=u'portrait.jpg')

class ISouscripteur(form.Schema):
    """
    Souscripteur Schema
    """
    form.widget(portrait=NamedImageFieldWidget)
    portrait = NamedImage(
        title=_(u"Portrait"),
        required=False,
        defaultFactory=get_portrait
    )

I can upload an image, that works. But when I load the form and get the portrait, I just see an empty input file. The data are comming from a external webservice.

I have tested to write an updateWidgets method to update the widget value with a NamedImage ( according to the doc ), but the result is the same.

With the debugger, I have found that the widget value still have a NamedImage in it, the updateWidgets method just replace it.

Is there is something I missed to show the image and the radio buttons in this widget ?

What you need is a computed field and, according to plone.directives.form documentation, z3c.form has the concept of a value adapter , a component that can provide a value for an attribute.

The following is an example for setting a title field with a default value using the first; try modifying it for you use case.

from plone.directives import form
from zope import schema

class IMySchema(form.Schema):

    title = schema.TextLine(title=u"Title")

@form.default_value(field=IMySchema['title'])
def default_title(data):
    return data.context.suggested_title

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM