Re: Passing variables to view

Ralph Heinkel <ralph.heinkel-S0/[email protected]> Mon, 19 Dec 2016 08:09:04 +0100
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
Hi Germán,

your index function could look like this:

@cherrypy.expose

def index(self):

c = cherrypy.thread_data.db.cursor()

c.execute('select count(*) from Website')

res = c.fetchone()

c.close()

html = open("index.html").read()

return html % res[0]

While your index.html is just:

<html>

<head></head>

<body>

Hello, you have %d records in your table.

</body>

</html>

However I would reccomend that you look into something like jinja2
for rendering your templates, make things a lot easier.

Ralph

On 12/18/2016 10:28 PM, Rimoldi wrote:

Hi Guys!

I'm getting started with CherryPy. I'm finding the learning
curve pretty steep for a newbie :(

I have a very simple question: How do I pass variables to
an html view?

class Root:

@cherrypy.expose

def index(self):

c = cherrypy.thread_data.db.cursor()

c.execute('select count(*) from Website')

res = c.fetchone()

c.close()

open("index.html")

cherrypy.quickstart(Root())

My index.html, being:

<html>

<head></head>

<body>

Hello, you have %d records in your table %res[0]

</body>

</html>

How should I rewrite my index function, to render the
variables to my html file?

--

You received this message because you are subscribed to the Google
Groups "cherrypy-users" group.

To unsubscribe from this group and stop receiving emails from it,
send an email to cherrypy-users+unsubscribe-/[email protected] .

To post to this group, send email to [email protected] .

Visit this group at https://groups.google.com/group/cherrypy-users .

For more options, visit https://groups.google.com/d/optout .

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/[email protected] .

To post to this group, send email to cherrypy-users-/[email protected] .

Visit this group at https://groups.google.com/group/cherrypy-users .

For more options, visit https://groups.google.com/d/optout .