【问题标题】:Trouble with python3 web.pypython3 web.py的问题
【发布时间】:2017-05-16 01:00:40
【问题描述】:

我正在学习 Python 在线课程并尝试使用 web.py 构建网站。我在 Windows 10 机器上运行 python 3.6.1。我能够根据需要手动安装 web.py 并验证它是否正确导入。我已经从 github 尝试了 web.py 的“python3”和“p3”分支,都导致了同样的问题。

我认为这是一组简单的三页定义,如下面的“urls”声明所示。当我运行代码然后转到我的浏览器并输入http://localhost:8080/ 时,我希望看到主页。但是,我得到随机结果,好像 web.application() 调用随机选择了 url 中的两个元素。我得到以下任何结果:

 404 - Not found
 500 - Key Error: '/register'
 500 - Key Error: '/postregistration'
 200 - Returns the Home page
 200 - Returns the Registration page
 200 - Returns the PostRegistration page

请注意,我从未输入过 http://localhost:8080/register/postregistration,但有时浏览器会像我一样呈现这些页面。

我无法理解它在做什么,我以为我是在逐行遵循讲师的示例。有什么想法吗?

import web
from Models import RegisterModel

urls = {
    '/', 'Home',
    '/register', 'Register',
    '/postregistration', 'PostRegistration'
}

render = web.template.render("Views/Templates", base="MainLayout")
app = web.application(urls, globals())

#Classes/Routes

class Home:
    def GET(self):
        return render.Home()

class Register:
    def GET(self):
        return render.Register()

class PostRegistration:
    def POST(self):
        data = web.input()

        reg_model = RegisterModel.RegisterModel()
        reg_model.insert_user(data)
        return data.username

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

【问题讨论】:

    标签: python web.py


    【解决方案1】:

    您的urls 变量是set。它应该是tuple。从此更改您的代码:

    urls = {
        '/', 'Home',
        '/register', 'Register',
        '/postregistration', 'PostRegistration'
    }
    

    到这里:

    urls = (
        '/', 'Home',
        '/register', 'Register',
        '/postregistration', 'PostRegistration'
    )
    

    【讨论】:

    • 非常感谢!这解决了我的问题。
    • @DaCone 如果答案解决了问题,接受它是一个好习惯。
    最近更新 更多