我正在用龙卷风编写简单的购物应用程序。我的路径如下所示:
Template = Application([
(r"/order/(.*)", PlaceOrder),
(r"/form", Form),
(r"/", Welcome),
], static_path=static_path, debug=True)
在用户发布“登录信息”后,我想重定向到Form
,然后在他填写自己的数据后,我想重定向到PlaceOrder
。
但是Form
未发布且Welcome
正在运行,因此我无法进入PlaceOrder
,而是被重定向到Form
。
这是我的课程(重要部分):
class Welcome(TemplateHelper, RequestHandler):
"""
'Login' page. It checks if this user exists
TemplateHelper is seting paths and initiating requester.
"""
def get(self):
self.render(self.template_welcome_path, title=u"Please fill in
your data.")
def check_user_exists(self, account, name, email):
if not all([account, name, email]):
return False
result = self.requester.check_user(account, name, email)
return result
def post(self):
try:
account = self.get_argument('account', None)
name = self.get_argument('name', None)
email = self.get_argument('email', None)
user_exists = self.check_user_exists(account, name, email)
if user_exists:
user_secret = self.requester.get_user_secret(account)
self.redirect('/order/{}'.format(user_secret), status=303)
return
else:
self.redirect('/form', status=303)
return
except Exception as e:
log.error('Failed login: {}'.format(e))
class PlaceOrder(TemplateHelper, RequestHandler):
def get(self, secret):
account, email = self.requester.get_data_from_secret(secret)
self.render(self.template_order_path, title='Please place your
order.', account=account, email=email)
def post(self, args):
self.write('Order placed')
class Form(TemplateHelper, RequestHandler):
"""
It displays form template and collects data from it.
"""
def get(self):
self.render(self.template_form_path,
title=u"Fill in the form.")
async def post(self):
reference = uuid.uuid4().hex
account = self.get_argument('account', '')
try:
writeonce = False
while True:
time.sleep(10)
status = await self.requester.check_status(reference)
if status == 'accepted':
user_secret = self.requester.get_user_secret(account)
self.redirect('/order/{}'.format(user_secret))
break
if status == 'declined':
self.render(self.template_msg_path,title=u"Verification status", message=status)
break
elif not writeonce:
self.write(
'Please wait, processing.')
self.flush()
writeonce = True
except Exception as se:
self.render(self.template_msg_path,title=u"Error", message=se.message)