Re: Modifying a page and not creating a new one
Denis LE FUR <[email protected]>
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
Thank you Tim !
I did my homework. More or less ...
---------------------------------
import cherrypy
import os
class monSite:
#---------------index
@cherrypy.expose
def index(self, a=0, b=0, btn=None):
if btn:
# We were asked to do a computation.
r = str(int(a)+int(b))
s = """
<form action="index" method="GET">
<input type=number name='a' value= %s> +
<input type=number name='b' value= %s>
<input type=submit name=btn value='='> %s
</form>
"""%(a,b,r)
else:
# initial page
s = """
<form action="index" method="GET">
<input type=number name='a' value=0> +
<input type=number name='b' value=0>
<input type=submit name=btn value='='>
</form>
"""
return s
cherrypy.quickstart(monSite())
---------------------------------
Le mercredi 27 avril 2016 14:17:04 UTC-3, Tim Roberts a écrit :
>
> Denis LE FUR wrote:
>
> Hello, I am a new user...
>
> Here is an example. The result appears in a new page. How to put it in the
> index page ?
>
>
> To start, you need to change the <form> tag so that action="index".
>
> Now you need some way to tell whether you're being called to display the
> blank page, or to display the results. You can do that by assigning a name
> to your "submit" button. Then you can check that value in the code. If it
> is present, then you are being asked to compute. If not, then it is an
> initial display. For example:
>
> def index(self, a=0, b=0, btn=None):
> s = ""
> if btn:
> # We were asked to do a computation.
> r = int(a)+int(b)
> s += "<p>Result of %d + %d is %d" % (a, b, r)
> s += """
> <form action='index' method='GET'>
> <input type=number name='a'> +
> <input type=number name='b'>
> <input type=submit name=btn value='='>
> </form>
> """
> return s
>
> The next exercise for you is to change the code so that the "a" and "b"
> fields on the form show the previous values.
>
> --
> Tim Roberts, [email protected] <javascript:>
> Providenza & Boekelheide, Inc.
>
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.