[英]I'm able to change a letter in “username” to a capital letter and register still with the same username. How do I fix this?
I have user accounts consisting of a [display_name, username, email, password]. 我的用户帐户包含[display_name,username,email,password]。 When registering a person will have to enter these credentials.
注册人员时必须输入这些凭据。 The problem is if a user already have a username of 'Juice' another user can register with the username 'juice'.
问题是如果用户已经拥有'Juice'的用户名,则另一个用户可以使用用户名'juice'注册。 How can I prevent this?
我怎么能阻止这个?
For example twitter won't let a user register with the same handle even if they change a letter to capital. 例如,即使用户将字母更改为大写,Twitter也不会让用户注册相同的句柄。
I have it where certain things are set to unique in the database but that doesn't help that problem. 我知道在数据库中将某些事物设置为唯一但这对这个问题没有帮助。 I'll be honest I'm lost!
我会说实话,我迷路了! This is my first project even though it is walk through (to see how everything goes together) I've been putting my own spin on things.
这是我的第一个项目,即使它是通过(看看一切如何在一起)我一直在自己的东西旋转。
# models.py for user
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
display_name = db.Column(db.String(20), nullable=False)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False,
default='default.jpg')
password = db.Column(db.String(30), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
# what a user looks like
def __repr__(self):
return f"User('{self.display_name}', '{self.username}',
'{self.email}', '{self.image_file}')"
# forms.py for RegistrationForm
class RegistrationForm(FlaskForm):
display_name = StringField('Display Name', validators=
[DataRequired(),
Length(min=2, max=20)])
username = StringField('Username', validators=[DataRequired(),
Length(min=4, max=20)])
email = StringField('Email', validators=[DataRequired(),
Email()])
password = PasswordField('Password', validators=[DataRequired(),
Length(min=6, max=30)])
confirm_password = PasswordField('Confirm Password', validators=
[DataRequired(), EqualTo('password')])
submit = SubmitField('Sign Up')
# stops same user credentials from signing up
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first()
if user:
raise ValidationError('That username is taken. Please choose
another one')
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user:
raise ValidationError('That email address is taken. Please
choose another one')
I want my application to not let a person register with a username like another. 我希望我的应用程序不要让一个人注册用户名。 For ex.
对于前者 registered username 'juice' a user can still register with the same username if they capitalize a letter like 'Juice"
注册用户名'juice'如果用户仍然可以使用相同的用户名注册,例如'Juice'
Piggy backing off of what @Shiot said. 小猪退出@Shiot所说的话。 Make the username lowercase when validating also update your saving function which is not in the code snippet provided.
在验证时将用户名设置为小写, 同时更新保存功能,该保存功能不在提供的代码段中。
def validate_username(self, username):
user = User.query.filter_by(username=username.data.lower()).first()
If I was you if you are in a production environment create a new field called display_name
make that case sensitive, copy all of the old usernames to this field, then make all usernames lowercase. 如果您是在生产环境中,则创建一个名为
display_name
的新字段,使该区分大小写,将所有旧用户名复制到此字段,然后将所有用户名设置为小写。
Nice name btw :) 好名字顺便说一句:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.