[Zope-DB] Hello!

Jim Penny jpenny@universal-fasteners.com
Mon, 21 Jul 2003 12:56:17 -0400


On Mon, 21 Jul 2003 18:15:38 +0200
Charlie Clark <charlie@begeistert.org> wrote:

> 
> On 2003-07-21 at 17:44:42 [+0200], Philip Kilner wrote:
> > res=container.insertBook(request)
> > 
> > ...seems to be accepted, but inserts nothing.
> > 
> > > In the second, it would look like
> > > 
> > > res=container.my_sql_method(foo=foo_value, bar=bar_value)
> > > 
> > 
> > OK, well the 2nd looks like a better bet - more explicit!
> > 
> > However, I can't save even a ZPT with that syntax -
> > 
> > res=container.insertBook(ISBN=field_ISBN, Title=field_Title)
> > 
> > 
> > ...gives me: -
> > 
> > Compilation failed
> > TAL.HTMLParser.HTMLParseError: junk characters in start tag: ', 
> > Title=field_Title)', at line 10, column 1
> > 
> > I actually can't se what I am doing wrong.
> 
> Nothing really and I hope Jim will forgive me but I didn't find the
> orignal explanation very helpful for someone getting just started.

Nothing to forgive.  I agree.  I misread the initial email, somehow
thought it refered to Script (python). 

> 
> First of all, do not ever use an UPDATE or an INSERT directly from ZPT
> or DTML or any other form for that matter. It is very bad practice and
> could cause you any kind of problems. Always use a PythonScript to do
> this for you so you can do your error checking gracefully.

Agreed.  ZPT is for Presentation.  Updates and Inserts do not present!

> 
> What you want to do can't be done anyway and has nothing to do with
> ZPT.

Well, it can be done, but is 1) a perversion, and 2) likely to lead to
other problems.

[Lots of concrete example removed.]

I will give another example, that shows how I would do this.

0)  I like the "Red Queen", "running in place" model.  (Damn I need to
write a howto on this.)

1)  index_html is ALWAYS a Script (Python) in any of my code from the
last 18 months.

2)  The main form will have ZPT in it, only to do error handling.

The ISBN_entry_form_pt  [the tal presents an error message, if present,
and fills in the form with the most recent values if said values are in
the request.  This should happen only after an error.]:

<html>
 <head><title>Enter ISBN Data</title>
 </head>
 <body>
 <h2>Enter ISBN Data</h2>

 <p color:red tal:content="options/error|nothing"></p>

 <form name="insert_book" action="." method="POST">
   <input type="text" name="field_ISBN"
      tal:content="request/field_ISBN|nothing">   <br>
   <input type="text" name="field_Title"
      tal:content="request/field_Title|nothing">   <br>
   <input type="text" name="field_Author"
      tal:content="request/field_Author|nothing">    <br>
  <input type="text" name="field_BookPrice"
      tal:content="request/Field_BookPrice|nothing">
  <input type="submit" name="next_state" value="Save ISBN Data">
 </form>
 </body>
</html>

index_html looks like this (note:  per above, it is a Script (Python)):

if not request.has_key('next_state'):
  return main_menu_form_pt(context, request)
  # main menu allows used to choose among usual CRUD operations
  # (CReate, Update, Delete), and probably offers reports
next_state=request['next_state']
if next_state=='Create ISBN Entry":
  return main_menu_form_pt(context, request)
elif next_state=='Save ISBN Data':
  error=container.check_save_ps()
  if error:
    return ISBN_Entry_form_pt(context, request, error=error)
  else:
    container.insert_ISBN_sql(field_ISBN=request['field_ISBN'],
      field_title=request['field_Title'],
      field_author=request['field_Author'],
      field_BookPrice=request['field_BookPrice'])
    return main_menu_form_pt(context, request)
      # You may want to return something else, if you are entering
      # in batches, it is better to return ISBN_entry_form_pt, for
      # example, but then you need breadcrumbs at the top of the form
      # so the user can get out of the loop.

main_menu_form_pt may be as simple as:

<html>
<body>
  <head>
    <title>Choose Action</title>
  </head>
  <form method="." action="post">
    <input type="submit" name="next_state" value="Create ISBN Entry">
    <input type="submit" name="next_state" value="Edit ISBN Entry">
    <input type="submit" name="next_state" value="Delete ISBN Entry">
  </form>
</html>


3)  This is really OffTopic for zope-db.  But hope it helps.

Jim Penny