Formlib edit forms

Posted by Graham Stratton Wed, 17 May 2006 20:51:00 GMT

There seems to be a bit of a shortage of simple documentation for formlib, so I’ll attempt to do my bit. This one’s just a custom edit form. Replacing the standard edit form allows you to put text around the form, and to change what actions can be executed on submit. In this case, we extend the render method to redirect to the object’s default view after editing, instead of returning to the edit view.

from zope.app.pagetemplate import ViewPageTemplateFile
from zope.formlib import form
from interfaces import ITextPage

class EditView(form.EditForm):
    form_fields = form.Fields(ITextPage)

    base_template = form.EditForm.template
    template = ViewPageTemplateFile('textpageedit.pt')

    def render(self):
        if self.errors is None or self.errors:
            return super(EditView, self).render()
        self.request.response.redirect('.')

As you can see, the default template is replaced, but is made available under the name ‘base_template’. This means that it can be accessed from the replacement page template like this:

<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:tal="http://xml.zope.org/namespaces/tal" 
      xmlns:metal="http://xml.zope.org/namespaces/metal" 
      metal:use-macro="view/base_template/macros/main">

<body>
<div class="section" metal:fill-slot="extra_info">
<h3>Edit this page</h3>
<p>Here you can put some instructions about editing the page.</p>
</div>
</body>
</html>

Take a look at the base template at formlib/pageform.pt to see what other METAL slots are available.

Posted in  | no comments

Comments

(leave url/email »)

   Comment Markup Help Preview comment