-
Notifications
You must be signed in to change notification settings - Fork 1
Profile Update Form
Manish Sah edited this page Oct 3, 2020
·
1 revision
lets create a ProfileUpdateForm in forms.py so that we can create forms for the user to allow them to update their profile
blog/forms.py
.
from flask_wtf.file import FileField, FileAllowed
.
class ProfileUpdateForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=30)])
email = StringField('Email', validators=[DataRequired(), Email()])
picture = FileField('Update Profile Picture', validators=[FileAllowed(['jpg', 'png'])])
submit = SubmitField('Update')
def validate_username(self, username):
if current_user.username != username.data:
user = User.query.filter_by(username=username.data).first()
if user:
raise ValidationError('That username is taken. Please choose different one')
def validate_email(self, email):
if current_user.email != email.data:
user = User.query.filter_by(email=email.data).first()
if user:
raise ValidationError('That email is taken. Please choose different one')
here, we have import FileField to allow user to upload their profile picture. Also while uploading picture we need some sort of validation on types of images, the user can upload. For that, we will import FileAllowed.
Now that we have Profile update form, lets pass this in our proifle route
.
from blog.forms import ProfileUpdateForm
.
@app.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
form = ProfileUpdateForm()
return render_template('profile.html', title_name='profile', form=form)
Implementation can be found over this commit