title: FlaskForm Validation Code: checking if a user already exists or not
body: <p>So I was following Corey Schafer's Flask tutorial when I came to the part about checking if a username or email is already used while registering for an account. I cannot for the life of me understand how this code works, I guess its because I'm new to Python and Flask but still it shouldn't be this confusing.</p>
  
  <pre><code>class RegistrationForm(FlaskForm):
  
      username = StringField('Username', validators=[DataRequired(), Length(min=4,                                  
      max=20)])
      email = StringField('Email', validators=[DataRequired(), Email()])
      password = PasswordField('Password', validators=[DataRequired()])
      password_confirm = PasswordField('Confirm password', validators=[DataRequired(),EqualTo('password')])             
      submit = SubmitField('Sign 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.')
  
  def validate_email(self, email):
      user = User.query.filter_by(email=email.data).first()
      if user:
          raise ValidationError('That email is taken. Please choose another.')
  </code></pre>
  
  <p>So, the specific part that I don't understand are the two functions in the RegistrationForm class: Firstly, how do both of the functions know what username and email is? For example if I changed the 'username' and 'email' arguments in the function definitions to something like 'u_n' and 'e_mail' and then ran the query with 'u_n.data' and 'e_mail.data', the code still works.
  Secondly if I'm to rename the functions to anything else the code stops working and I don't understand why?</p>
  
  <p>I know the answer to this cannot be too complicated but this has annoyed me so much. Any help would be greatly appreciated.</p>