Search code examples
pythoncherrypy

Possible to send variables to the index page in CherryPy?


For instance I want to visit http://localhost:8080/?var=val or similar with POST, but I get a 500 server error:

500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last):
 File "c:\python26\lib\site-packages\cherrypy\_cprequest.py", line 606, in respond
   cherrypy.response.body = self.handler()
 File "c:\python26\lib\site-packages\cherrypy\_cpdispatch.py", line 25, in __call__
   return self.callable(*self.args, **self.kwargs)
TypeError: index() takes no arguments (1 given)

Powered by CherryPy 3.1.2


Solution

  • It is definitely possible.

    Here is an example (adapted from the CherryPy tutorial):

    <form action="indexPostHandler" method="post">
        <p>Enter a value:</p>
        <input type="text" name="val" value=""/>
        <p><input type="submit" value="Login"/></p>
    </form>
    

    And, in your index, you can use something like the following to process the request:

    class Root:
        # create form here
        def indexPostHandler(self, val=None):
            # do something with val here
            ...