[Zope] First post: nested <dtml-in> tags looping through python lists

john jacobs johnj@ucsc.edu
Tue, 15 Jan 2002 11:40:21 -0800 (PST)


Hello All,

	This is my first post.  Please excuse me if this question / issue
has been posted before -- I would appreciate a reference to the previous
post if it has.

	I am calling an external python script from a dtml document.  This
external script performs some database queries and returns the result set
as a list of lists (a list of rows, each row containing a list of fields).
I would like to nest one <dtml-in> tag within another, so as to display
the value of each field for each row in the result set.  My existing code
looks like this:

	dtml document:

	<table border=1>
	<dtml-in expr="retrieveData(REQUEST)">
	<tr>
	<dtml-in sequence-item prefix="row">
		<td><dtml-var row-item></td>
	</dtml-in>
	</tr>
	</dtml-in>
	</table>


The external method constructs the list of lists like this:

    ## database connection and query code removed -- variable "cursor" is
    ## a database cursor pointing to a MySQL result set

    curdesc = cur.description
    records = cur.fetchall()
    outrecords = []

    for nextrecord in records:
        newresult = {}
        i = 0
        for nextcolumn in nextrecord:
            key = curdesc[i][0]    ## column name for the current field
            newresult[key+"_type"] = curdesc[i][1]
            newresult[key] = value
            i = i + 1
        outrecords.append(newresult)
    return outrecords


Now, when the SQL query returns zero results, the dtml document correctly
displays nothing.  However, whenever at least one row *is* returned from
the query (and thus my list of lists contains at least one row), I get the
following zope error:

 Error Type: KeyError
 Error Value: 0


Now, my questions are:  Has anyone attempted to do this before?  Is it
feasable?  If a list of lists is not the correct data structure to use for
nested <dtml-in> tags, what is the correct data structure and how can I
construct one in an external python method?

Thanks in advance,

--john