Am 26.02.2011 um 02:56 schrieb vhiremath4:


So I have a really noob-like question, and I am very sorry for it. My friend and I have no prior experience with frameworks. We simply know HTML, CSS, and JavaScript(jQuery), as well as Python (obviously) and how to access
information from a database we set up using Python.

Anyway, I was wondering how you display the return of a .py file in your "controllers" folder. I have set up a .py file with a function inside that
returns a table. Here's the code:

from sqlalchemy import *

def rdshinfo():
   #Database access
   honudb=create_engine('mysql://DBNAME:PASS@HOSTNAME/DBNAME')
   conn=honudb.connect();
   query=select("*",from_obj=["shows"])
   result=conn.execute(query)
   return result

I would like to print information from that table onto my index.html
template using the following loop:

if result.rowcount>0:
   for r in result:
       print 'series:  '+r.series
       print 'title:   '+r.title
       print 'poster:  '+r.poster

I tried setting up a function in the root.py file in "controllers" as
follows:

@expose('honutv.templates.index')
   def get_shinfo(self):
       result = rdshinfo()
       return result

I figured that I should at least get a feel for how to display the table on my template. However, I believe I haven't set up my function in root.py properly. Also, I am not sure how to call this function in index.html from this point. I'm very grateful for any responses. Again, sorry for the very
noob-like question. Truth is, I hate reading books on Pylons and other
material because I pick up new software so much more quickly by just
exploring it and learning from my mistakes.


You return values to display as a *dict*. So in your example, something like this:


@expose(...)
def get_shinfo(..):
   return dict(results=rdshinfo())


Then in the template you have a variable name "results" to your availability, which you deal with according to the template language. I use Genshi, and that would look like this:


<ul>
  <li py:for="row in results">
    ${row.title}, ${row.poster}
 </li>
</ul>

Diez

--
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to