简体   繁体   中英

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]. 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'. 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.

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"

Piggy backing off of what @Shiot said. 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.

Nice name btw :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM