Reputation: 11
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()
Upvotes: 1
Views: 515
Reputation: 663
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'
)
Upvotes: 2