Most basic way to display Data from Database in a HTML table row-by-row

7 views
Skip to first unread message

SMERSH009

unread,
Jul 3, 2007, 3:49:11 AM7/3/07
to TurboGears
Hi All,
I am trying to display Data from a database table to display in a
Table format.
Right now the results are just being bunched up together. For example,
when displayed in the web browser the ${ids} returns 1922 instead of
19
22
Now I know that this seems like a basic question, but I looked all
over the tutorials, and they seem to concentrate on displaying ALL the
data at once, and not to display one row at a time.

model.py contains:

class WebAppPages(SQLObject):
class sqlmeta:
style=Style(longID=True)
idName='pageID'

pageName= UnicodeCol()
url = UnicodeCol(notNone=True)
post = BoolCol(default=False)

controllers.py contains. And I am sure this is wrong...:

class Root(controllers.RootController):
@expose(template="formstutorial.templates.index")
def index(self):
result= WebAppPages.select()
names = map(lambda x: x.pageName ,list(result))
urls = map(lambda x: x.url , list(result))
ids = map(lambda x: x.id , list(result))
return dict(names=names,urls=urls,ids=ids)

index.kid contains:

<html>
<head><title>Webapptests</title></head>
<body>

${urls}
${names}
${ids}
</body>
</html>


Thank you!

alex bodnaru

unread,
Jul 3, 2007, 4:09:09 AM7/3/07
to turbo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


hi,

i'm about to finish a small piece of code that will display all rows in
a table of your choice, with the option to also add, delete and update
rows through ajax.

hope to publish this today or tomorrow.

alex

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQCVAwUBRooEJdpwN1sq38njAQL7OwP6A9wBXY4ILqZicxBX+9e3vGO/JsdKVOhS
lJHkE9iMizci6lPEbNczpYNqLLRw1sPB7R46UwWq3xKccfwX/PbITc3nHJred3bB
KK32cuBVezweeScm9Qc367/8EaCEy2Sswo6yW0pG5+Oje0qvIzxBjKE7zfC0DoCF
/CcEmP6SxIM=
=cvPo
-----END PGP SIGNATURE-----

Johnny Blonde

unread,
Jul 3, 2007, 7:12:47 AM7/3/07
to TurboGears
controller:
-------------------------------------
results = TableToDisplay.select()
return dict(results=results)

template
------------------------------------------


<html>
<head><title>Webapptests</title></head>
<body>

<table>
<tr py:for="row in result">
<td py:content="row['name']">
<td py:content="row['url']">
<td py:content="row['id']">
</tr>
</table>

</body>
</html>
---------------------------------------------
doesn´t this work?

SMERSH009

unread,
Jul 3, 2007, 12:33:27 PM7/3/07
to TurboGears
Hi Johnny, your code was close, but is returning an error in the .kid
file
<tr py:for="row in result"> This line returns: the prefix "py" for
attribute "py:for" associated with an element type tr
is not bound

Alex, since having to edit these rows was going to be the next step
in this difficult project (I am a newbie to Python), you are going to
be my lifesaver if your code will work. I will keep checking the
thread for your guys' updates.

On Jul 3, 4:12 am, Johnny Blonde <frank.wagner.1...@googlemail.com>
wrote:

Jim Steil

unread,
Jul 3, 2007, 2:42:32 PM7/3/07
to turbo...@googlegroups.com
In the sample below you'll need to change the 'result' to 'results' in the html portion.  That is what is being returned in the dict.

    -Jim

SMERSH009

unread,
Jul 3, 2007, 4:33:14 PM7/3/07
to TurboGears
Sorry Jim, I should have specified that this error pops up whether or
not I have it set to 'result' or 'results.'

"the prefix "py" for attribute "py:for" associated with an element
type tr is not bound "

Turbogears1.0 Python v2.5.1c
Here is what the template file looks like in my code to match the
columns in my DB.
Thanks...

<html>
<head><title>Webapptests</title></head>
<body>

<tr py:for="row in results">
<td py:content="row['pageID']">
<td py:content="row['pageName']">


<td py:content="row['url']">

</tr>
</table>

</body>
</html>

Alaa Salman

unread,
Jul 3, 2007, 5:41:10 PM7/3/07
to turbo...@googlegroups.com
I believe that you need to specify the proper namespace in the kid template and give it the "py" alias. Check the example in any quickstart application.

Regards,
Alaa Salman

Jim Steil

unread,
Jul 3, 2007, 5:47:37 PM7/3/07
to turbo...@googlegroups.com
In no way do I claim to understand why/how all of this works, but in all of my page templates my html tag looks something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#"
    py:extends="'master.kid'">

    -Jim

SMERSH009

unread,
Jul 3, 2007, 9:00:46 PM7/3/07
to TurboGears
Ahh, I finally got it... with your guys' help of course :)
This was the way to display the Column value of a specific row: <td
py:content="row.pageName"></td>
The format: <td py:content="row['name']"> was throwing
errors for me.
The final product is below...
I am still eagerly awaiting for the kind of good stuff Alex has in
mind.

Happy 4th of July everyone!!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/
kid/ns#"
py:extends="'master.kid'">

<head>

<title>Webapptests</title></head>
<body>

<table border="1">


<tr py:for="row in results">

<td py:content="row.id"></td>
<td py:content="row.pageName"></td>
<td py:content="row.url"></td>
</tr>
</table>

</body>
</html>

Reply all
Reply to author
Forward
0 new messages