python - web.py 脚本一直说, "' return'outside function”

标签 python error-handling web.py

每当我尝试使用 web.py 运行此脚本时,我都会收到错误消息

<type 'exceptions.SyntaxError'> at /hello
'return' outside function (app.py, line 19)

这是我的来源:

import web
urls = (
'/hello', 'Index',
'/file_upload_form', 'ThatFile'
)

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

render = web.template.render('templates/', base = "layout")

class ThatFile(object):
    def GET(self):
        return render.file_upload_form()

    def POST(self):
        form = web.input(image = "loc")
        image_o = open(form.image,'r')
        return render.thatfile(image_o = image_o)

class Index(object):

    def GET(self):
        return render.hello_form()

    def POST(self):
        form = web.input(name = "Nobody", greet = None)
        greeting = "%s, %s" % (form.greet, form.name)
        return render.index(greeting = greeting)

if __name__ == "__main__":
    app.run()

我尝试重新缩进“返回”,但我只是收到该消息。

最佳答案

你的代码混合了制表符和空格,这让 Python 感到困惑。您可以选择使用制表符,也可以选择使用空格(PEP 8 推荐空格),但必须保持一致。

检查您的编辑器设置,看看是否可以让它以不同的方式显示制表符和空格(大多数编辑器都可以做到这一点)。

关于python - web.py 脚本一直说, "' return'outside function”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6805344/

相关文章:

c++ - 尽管 namespace 正确,但重复出现错误

python - 如何将 Python 项目链接到 WSGI 文件?

python - 有没有办法在 Python 中识别继承的方法?

django - 在 Django 表单中重载错误消息

python - Python-测试错误

来自新手的 Web.Py 中的 Python 错误

python - 是什么阻止我在 web.py 框架中使用 CSS 样式表?

python - SqlAlchemy - PostgreSQL - 连接错误信息的 session

以任意精度实现 ROUND_HALF_UP float 的 Pythonic 方法

python连接到docker外的postgres docker实例