2

I'm in the context of a BrowserView. I want to render a form inside the page.

class CommandeView(BrowserView):
    ...
    def render_myform(self):
        livraison_form = LivraisonForm(self.context, self.request)
        livraison_form.update()
        return livraison_form.render()

class ILivraisonForm(interface.Interface):
    livraison = schema.Choice(title=_(u"Mode de livraison"),
                          vocabulary=livraison_vocabulary)


class LivraisonForm(AutoExtensibleForm, form.EditForm):
    schema = ILivraisonForm

class LivraisonFormAdapter(object):
    component.adapts(ICommande)
    interface.implements(ILivraisonForm)
    def __init__(self, context):
        self.context = context
        self.livraison = self.context.livraison

I would like render_myform to render only the form but it return a HTML Plone page.

Any tips are welcomed.

2
  • Have you tried with a FormWrapper ? I'm not sure it works in the case of a browserview
    – Jihaisse
    Aug 17, 2012 at 10:10
  • The formwrapper is to create a page to display the form. I want the opposite: display a form inside an existing page.
    – toutpt
    Aug 17, 2012 at 12:00

1 Answer 1

2

Haven't checked this in-depth, but the form is probably wrapped by the plone FormWrapper class.

Look for a .form attribute on the LivraisonForm instance:

def render_myform(self):
    livraison_form = LivraisonForm(self.context, self.request).form
    livraison_form.update()
    return livraison_form.render()

It may be you need to add an additional interface to the request for this to work (the wrapper normally does this for you. I'm a little low on time right now, so you get this untested:

from plone.z3cform import z2

class CommandeView(BrowserView):
    def render_myform(self):
        z2.switch_on(self)  # Apply layer interface, set up locale, add `getURL` method
        livraison_form = LivraisonForm(self.context, self.request).form
        livraison_form.update()
        return livraison_form.render()

The Plone Knowledgebase uses this same setup to show a form in a viewlet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.