BahmanM
BahmanM

Reputation: 1445

Wiring template form actions with Python code

I'm working on a website that has a “Sign up” page which should be callable from anywhere in the site.

I have the following dummy interface and implementation for the “user” product:

Interface:

 ##
 ## located in bahmanm/sampleapp/interfaces.py
 ##
 class ISampleAppUser(Interface):
        """
        """

Implementation:

 ##
 ## located in bahmanm/sampleapp/implementation/SampleAppUser.py
 ##
 class SampleAppUser:
      """
      """

      implements(ISampleAppUser)

 # Note that this method is outside of the implementation class.
 #
 def manage_addSampleAppUser(self, id, title):
    # ...

Now, for the moment, let's assume there's a link on the index page which leads to the following template (Sign up template):

 <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns="http://xml.zope.org/namespaces/tal">
   <head><title>Add a new User</title></head>
   <body>
    <h2>Add a user instance</h2>
    <form action="#" method="POST"
          tal:attributes="action python:'manage_addSampleAppUser'">
       <p>Id: <input type="text" name="id"/></p>
       <p>Title: <input type="text" name="title"/></p>
       <input type="submit" value="Add"/>
    </form>
   </body>
  </html>

However I haven't been able to find the right value for action property of the form; all I get is a “resource not found”.

Honestly, I believe it's a problem of understanding Zope's mechanisms on my side. I'd really appreciate any hints/clues on where should I go digging for the solution, configure.zcml or the implementation or the template itself. TIA,

Upvotes: 0

Views: 404

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125058

You really want to create a view for that; you can call a Product factory like that from a URL too, but it is not recommended.

With a view, you can combine the form and the code to create the new user in one place:

from zope.publisher.browser import BrowserPage
from sampleapp.implementation.SampleAppUser import manage_addSampleAppUser


class NewUserSignup(BrowserPage):
    def __call__(self):
        # called when the view is being rendered
        if 'submit' in self.request:
            # form was submitted, handle
            self.addUser()
        return self.index()  # render the template

    def addUser(self):
        # extract form fields from self.request.form
        # validation, error handling, etc.
        if someerror:
            self.error = 'Error message!'
            return

        user = manage_addSampleAppUser(self.context, id, title)
        # add things to this new user if needed

        # all done, redirect to the default view on the new user object
        self.request.response.redirect(user.absolute_url())

then register this view with something like:

<browser:page
    for="*"
    name="signup"
    class=".signup.NewUserSignup"
    template="signup.pt"
    permission="zope.public"
    />

When your new page is registered, the named template is added as a index attribute on your NewUserSignup class, so the __call__ method can invoke it (self.index()) and return the results.

Because you combined the signup handling and the template together, you can now easily incorporate error handling. When someone loads the page for the first time self.request.form will be empty, but as soon as someone hits the submit button, you can detect this and call the addUser method.

That method can either create the user and then redirect away from this page, or set an error message and return, at which point the form is re-rendered.

This makes the action easy to set; you could just leave it empty, or you can set it to the current context URL plus the name of the view. Together, the template then becomes:

<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns="http://xml.zope.org/namespaces/tal">
   <head><title>Add a new User</title></head>
   <body>
    <h2>Add a user instance</h2>
    <div class="errormessage" tal:condition="view/error|nothing" tal:content="view/error">
        Conditional error message appears here
    </div>

    <form action="#" method="POST"
          tal:attributes="action string:${context/absolute_url}/@@${view/__name__}">
       <p>Id: <input type="text" name="id"
                      tal:attributes="value request/form/id|nothing" /></p>
       <p>Title: <input type="text" name="title"
                      tal:attributes="value request/form/title|nothing" /></p>
       <input type="submit" value="Add" name="submit"/>
    </form>
   </body>
</html>

Note how the form inputs are pre-filled with existing data from the request as well, making it easier for your visitor to correct any errors they may have made.

Upvotes: 1

Related Questions