1

I'm taking an online class in python and trying to build a website using web.py. I am running python 3.6.1 on a Windows 10 machine. I was able to install web.py manually as required and verify that it is imported correctly. I have tried both the "python3" and "p3" branches of web.py from github and both result in the same problem.

I have what I believe to be a simple set of three pages defined as seen in the "urls" statement below. When I run the code then go to my browser and enter http://localhost:8080/, I expect to see the Home page. However, I get random results, as if the web.application() call is randomly picking two of the elements in urls. I get any of the following results:

 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

Note that I never entered http://localhost:8080/register or /postregistration, yet sometimes the browser would render those pages as if I did.

I can't make sense out of what it is doing and I thought I was following the instructor's examples line for line. Any thoughts?

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()

1 Answer 1

2

Your urls variable is a set. It should be a tuple. Change your code from this:

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

To this:

urls = (
    '/', 'Home',
    '/register', 'Register',
    '/postregistration', 'PostRegistration'
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! That took care of my problem.
@DaCone If the answer solved the issue, it is a good practice to accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.