【问题标题】:trying to run webpy example: app isn't defined尝试运行 webpy 示例:未定义应用程序
【发布时间】:2019-02-24 01:03:11
【问题描述】:

我正在尝试使用 webpy 实现基于别人代码的验证码。我开始的代码在这里:https://kzar.co.uk/blog/2009/07/14/web.py-captcha/

那里的示例代码不完整,我需要弄清楚如何处理这个 app 变量。这是我的代码:

import web
from captcha import getCaptcha

render = web.template.render('templates/')

urls = (
        '/([a-zA-Z]+/[a-zA-Z]+)', 'index',
        '/', 'index',
        '/captcha.gif', 'captcha'
        )

if web.config.get("_session") is None:
    session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'captcha': ''})
    web.config._session = session
else:
    session = web.config._session

vcaptcha = form.Validator('Please enter the code', lambda x:x == session.captcha)

enquiry_form = form.Form(
        form.Textbox("captcha", vcaptcha, description="Validation Code", pre="<img src='/captcha.gif' valign=center><br>", class_="standard", style="width:70px;"),
        )

class index:
    def GET(self, argu = "Anonymous/Person"):
        args = argu.split('/')
        firstname = args[0]
        if (len(args) >= 2):
            lastname = args[1]
            return render.index(firstname, lastname)
        return render.index(firstname, "Snow")

class captcha:
    def GET(self):
        web.header("Content-Type", "image/gif")
        captcha = getCaptcha
        session.captcha = captcha[0]
        return captcha[1].read()

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

运行时出现此错误:

$ python code.py
Traceback (most recent call last):
  File "code.py", line 13, in <module>
    session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'captcha': ''})
NameError: name 'app' is not defined

我一直在查看 webpy 文档和 API 参考,但我不知道如何正确初始化这个“app”变量。

【问题讨论】:

    标签: python web.py


    【解决方案1】:

    当您调用session = web.session.Session(app, ... 时,您正在使用尚未定义的app 你看过sessions 上的文档吗?在使用之前看看他们如何在示例中定义app

    【讨论】:

      【解决方案2】:

      在 URL 之后应该有这个:

      app = web.application(urls, globals())
      

      【讨论】:

        猜你喜欢
        • 2021-09-26
        • 1970-01-01
        • 2019-03-07
        • 1970-01-01
        • 1970-01-01
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多