Quantcast

Passing variables to view

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Passing variables to view

Germán Rimoldi
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing variables to view

Ralph Heinkel-2

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 [hidden email].
To post to this group, send email to [hidden email].
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing variables to view

Germán Rimoldi
In reply to this post by Germán Rimoldi
Much appreciated Raph! 

It works like a charm

:)



On Sunday, December 18, 2016 at 6:39:40 PM UTC-3, Germán 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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Loading...