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,
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:
then register this view with something like:
When your new page is registered, the named
templateis added as aindexattribute on yourNewUserSignupclass, 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.formwill be empty, but as soon as someone hits the submit button, you can detect this and call theaddUsermethod.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
actioneasy 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: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.