Web.py routing seems to keep breaking

2019-08-25 01:55发布

问题:

For some reason, every time I run my web app it comes up with new errors, either it can't find my pages, or Attribute errors, or KeyErrors and I don't know why. I'm following a tutorial written a while back, and some web.py rules may have changed, but I can't figure out why this isn't working. Here is my controller.py file:

import web

urls = {

    '/', 'Home',
    '/register', 'Register',
    '/postregistration', 'PostRegistration'
}

render = web.template.render('Views/Templates', base='MainLayout')

# 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()
        return data.username

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.internalerror = web.debugerror
    app.run()

My file structure looks as follows:

controller.py
Models/
static/
    css/ [css files]
    js/ [javascript (bootstrap, jquery)
Views/
    Templates/
        MainLayout.html
        Home.html
        Register.html

I'm developing on Windows, can someone help me out?

回答1:

Your urls is written using braces {} rather than the correct parentheses (). Braces make the items a set, which does not guarantee order. Parentheses make it a tuple which does guarantee order.

web.py goes through url, two-by-two, assuming the first is the url part and the second is the class to handle it. Without a guaranteed ordering, your results, as you see, are random.



标签: python web.py