在CherryPy中可以将变量发送到索引页面吗?

1 投票
1 回答
881 浏览
提问于 2025-04-15 17:34

比如说,我想访问 http://localhost:8080/?var=val 这样的链接,或者用POST方式访问,但我却遇到了500服务器错误:

500 内部服务器错误

服务器遇到了一个意外的情况,导致无法完成请求。

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)

由 CherryPy 3.1.2 提供支持

1 个回答

1

这绝对是可能的。

这里有一个例子(改编自CherryPy教程):

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

在你的索引文件中,你可以用下面这样的代码来处理请求:

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

撰写回答