本文整理汇总了Python中database.User.username_exists方法的典型用法代码示例。如果您正苦于以下问题:Python User.username_exists方法的具体用法?Python User.username_exists怎么用?Python User.username_exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database.User
的用法示例。
在下文中一共展示了User.username_exists方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from database import User [as 别名]
# 或者: from database.User import username_exists [as 别名]
def post(self):
username = self.request.get('user')
password = self.request.get('password')
password_repeated = self.request.get('verify')
email = self.request.get('email')
# Validate input and generate needed error messages
if not validations.is_valid_username:
username_error = 'Invalid Username'
elif User.username_exists(username):
username_error = 'That username is already in use.'
else:
username_error = ''
password_error = ('' if validations.is_valid_password(password)
else 'Invalid Password.')
password_repeated_error = ('' if (password_error or
password == password_repeated)
else 'Passwords do not match.')
email_error = ('' if (not email or validations.is_valid_email(email))
else 'Invalid Email Address.')
errors = [username_error, password_error,
password_repeated_error, email_error]
# If success, send to welcome page
if all(not error for error in errors):
# Success!
# Hash password
hashed_pwd = auth.make_pw_hash(username, password)
# Add to database
new_user_entry = User(username=username,
password=hashed_pwd,
email=email,
parent=ndb.Key('Users', 'user_group'))
# if email:
# new_user_entry.email = email
new_user_entry.put()
# Add cookie
self.force_login(username)
redirect_when_finished = self.request.get('redirect_when_finished')
return self.redirect_with_queries(
'/welcome',
[('redirect_when_finished', redirect_when_finished)])
else:
# If any checks failed, send back to the signup page
# with error messages. Repopulate fields except for passwords.
self.response.out.write(self.template.render(
entered_username=username,
email=email,
username_error=username_error,
password_error=password_error,
password_repeated_error=password_repeated_error,
email_error=email_error
))