[Zope-DB] writing a template to test if the sql worked
Charlie Clark
charlie at egenix.com
Tue May 4 11:55:25 EDT 2004
Hi Laura,
get DTML and *all* HTML scripting stuff out of your head; I think they are
the causes of lots of problems.
ZPTs are great because they encourage you to concentrate on your
presentation logic and break your procedural and data logic into external
components.
So what you should be doing is posting your form to a PythonScript which
does the error checking and informs the result template accordingly.
Your PythonScript will look something like this
request = context.REQUEST
errors = []
error_msg = ""
try:
context.add_pc_sql()
except SpecificError: """you will need to change this but I'm adding to
make a point"""
errors.append("database error")
error_msg += "Sorry the pc could not be added to the database"
request.set("errors", errors)
request.ser("error_msg", error_msg
return context.myZPT
And your ZPT might look something like this:
<body tal:define="errors request/errors | nothing">
<!-- use a stylesheet for your formatting -->
<p class="error"
tal:condition="exists: errors"
tal:content="structure request/error">Error message will be printed here</p>
<p tal:condition="not: errors">
The pc, <strong tal:content="request/pcname">PCNAME</strong> was
successfully added to the database.</p>
</body>
Notice how inflexible tal:condition is. Although this can be annoying it is
actually a "good thing". You really don't want control structures in your
presentation code. Simple binary cases: "either this or that" is as much as
you want.
Working like this will allow you to declare how specific things should be
handled - there respective success and failure messages allowing you to
reuse the code more easily. Note this is also how Formulator works so it
might be time to give that a spin.
Charlie
On 2004-05-04 at 17:31:33 [+0200], Laura McCord wrote:
>
> Hi,
>
> If I have an edit form and I click on edit after inserting the info
> into the fields I am redirected to a dtml method that I am using to call
> the zsql method and it tests if it was successful using the <dtml-try>
> and <dtml-except> conditons. This is the code that I am using:
>
> <html>
> <body bgcolor="#e7e7e7">
> <font color="#0a4f72" size="4">
> <dtml-if name="create">
> <dtml-try>
> <dtml-call name="add_pc_sql">
> The pc, <strong><dtml-var name="pcname">,</strong>
> was successfully added to the database.
> <dtml-except>
> Sorry, the pc was not added to the database.
> </dtml-try>
> </dtml-if>
> </font>
> </body>
> </html>
>
> I have been looking in the zope book and trying to learn how I could
> change this into a page template instead of a dtml method. Is there
> anything in page templates that can do the error checking like this dtml
> method? I wasn't sure if I would be using a <tal:condition> to achieve
> this.
>
> Thanks
More information about the Zope-DB
mailing list