我本想實(shí)現(xiàn)一個(gè)注冊(cè)用戶功能, web框架使用tornado 數(shù)據(jù)庫(kù)是使用mongodb但在注冊(cè)時(shí)出現(xiàn)redirect的exception。
class RegisterHandler(BaseHandler):
def get(self):
self.render_html('register.html')
@tornado.web.asynchronous
@gen.coroutine
def post(self):
username = self.get_argument('username')
email = self.get_argument('email')
password = self.get_argument('password')
captcha = self.get_argument('captcha')
_verify_username = yield self.db.user.find_one({'username': username})
if _verify_username:
self.flash(u'用戶名已存在', 'error')
self.redirect('/auth/register')
_verify_email = yield self.db.user.find_one({'email': email})
if _verify_email:
self.flash(u'郵箱名已存在', 'error')
self.redirect('/auth/register')
if captcha and captcha == self.get_secure_cookie('captcha').replace(' ',''):
self.flash('驗(yàn)證碼輸入正確', 'info')
else:
self.flash('驗(yàn)證碼輸入錯(cuò)誤', 'error')
self.redirect('/auth/register')
password = hashlib.md5(password + self.settings['site']).hexdigest()
profile = {'headimg':'', 'site':'', 'job': '', 'signature': '',
'github': '', 'description': ''}
user_profile = yield self.db.profile.insert(profile)
data = {'username': username, 'email': email, 'password': password,
'timestamp': time.time(), 'profile_id': str(user_profile)}
yield self.db.user.insert(data)
self.set_secure_cookie('user', username)
self.redirect('/')
這是代碼部分,本想如果用戶輸入驗(yàn)證碼出錯(cuò)就跳轉(zhuǎn)到注冊(cè)頁(yè)面。但是問(wèn)題是驗(yàn)證碼出錯(cuò)也會(huì)繼續(xù)執(zhí)行一下代碼。雖然在self.redirect后添加self.finish()會(huì)終止代碼,但 self.redirect本身不是會(huì)執(zhí)行一次finish操作么因?yàn)檫@個(gè)添加finish會(huì)報(bào)出執(zhí)行兩次self.finish()的錯(cuò)誤。
還有就是雖然驗(yàn)證碼出錯(cuò)后用戶還是注冊(cè)了, 而最后一行的self.finish不會(huì)被執(zhí)行,而是會(huì)跳轉(zhuǎn)到注冊(cè)頁(yè)面。 怎么解決這個(gè)問(wèn)題呢?
1 回答

偶然的你
TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
直接返回即可
return self.redirect('/auth/register')
或者
self.redirect('/auth/register')
return
添加回答
舉報(bào)
0/150
提交
取消