简体   繁体   English

我可以将“用户名”中的字母更改为大写字母,并使用相同的用户名进行注册。 我该如何解决?

[英]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.

相关问题 如何使 5 个字母的用户名成为要求? - How do i make a 5 letter username a requirement? 当且仅当前一个字母不是大写字母时,才如何在大写字母前插入空格? - How do I insert space before capital letter if and only if previous letter is not capital? 如何在 python 中使用 ascii 仅将首字母更改为大写 - How do I only change first letter into a capital using ascii in python 如何让我的代码将其中包含大写字母的单词的首字母大写? (猪拉丁语) - How do I make my code capitalize the first letter of the word that has a capital letter in it? (Pig Latin) 如何通过映射每个大写字母仅提取括号内首字母缩略词后的缩写 - How do i extract only abbreviation following acronyms inside the brackets by mapping each Capital letter 如何使用正则表达式提取第二个大写字母后的所有文本(数字、字母、符号)? - How do I extract with regex all the text (numbers, letters, symbols) after the second capital letter? 如何识别 Python 中是否至少有一个大写字母? - How can I identify if there is, at least, one capital letter in Python? 如何改变python中大写字母的单词的第i个字母? - how to change ith letter of a word in capital letter in python? 如何找到以大写字母开头的字符串中的单词? - how can i find the words in a string that start with capital letter? 如何修复此循环以进行简单的用户名和密码验证? - How do I fix this this loop for a simple username and password verification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM