User Registration Not Working In Heroku (Python Flask Framework)


I am trying to register a user in my blogging application. It works fine in local server. But when I deployed it to Heroku, it gives me 500 status code error.

Problem: Status Code 500 while trying to register a user in Heroku.

The code are as follows:

models.py

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64), index = True, unique = True)
    firstname = db.Column(db.String(50))
    middlename = db.Column(db.String(50))
    lastname = db.Column(db.String(50))
    dob = db.Column(db.DateTime)
    email = db.Column(db.String(120), index = True, unique = True)
    password_hash = db.Column(db.String(128))

    def __repr__(self):
        return "<User {}>".format(self.username)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

forms.py

class RegistrationForm(FlaskForm):
    username = StringField("Username", validators = (DataRequired(message = "Username is required.")), render_kw = {"placeholder": "Username"})
    firstname = StringField("First Name", validators = (DataRequired(message = "First Name is required.")), render_kw = {"placeholder": "First Name"})
    middlename = StringField("Middle Name", render_kw = {"placeholder": "Middle Name"})
    lastname = StringField("Last Name", validators = (DataRequired(message = "Last Name is required.")), render_kw = {"placeholder": "Last Name"})
    dob = DateField("Date Of Birth", validators = (DataRequired(message = "Date Of Birth is required.")), format = "%Y-%m-%d")
    email = StringField("Email", validators = (DataRequired(message = "Email Address is required.")), render_kw = {"placeholder": "Email Address"})
    password = PasswordField("Password", validators = (DataRequired(message = "Password is required.")), render_kw = {"placeholder": "Password"})
    confirm_password = PasswordField("Repeat Password", validators = (DataRequired(message = "Password Confirmation is required."), EqualTo("password")), render_kw = {"placeholder": "Confirm Password"})
    submit = SubmitField("Register")

    def validate_username(self, username):
        user = User.query.filter_by(username = username.data).first()
        if user is not None:
            raise ValidationError("Username Already Exists! Please Use A Different Username.")
    
    def validate_email(self, email):
        user = User.query.filter_by(email = email.data).first()
        if user is not None:
            raise ValidationError("Email Address Already Exists! Please Use A Different Email Address.")

routes.py

@app.route("/register", methods = ("GET", "POST"))
def register():

    if current_user.is_authenticated:
        return redirect(url_for("dashboard"))

    form = RegistrationForm()

    if form.validate_on_submit():
        user = User(username = form.username.data, email = form.email.data, firstname = form.firstname.data, middlename = form.middlename.data, lastname = form.lastname.data, dob = form.dob.data.strftime("%Y-%m-%d"))
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash("Congratulations! You are now a registered user.", "success")
        return redirect(url_for("login"))

    return render_template("register.html", title = "User Registration", form = form)

register.html

<form action="" method="POST">
    {{ form.hidden_tag() }}
    <div class="form-group">
        <input type="text" class="form-control" name="username" placeholder="Username">
        {% for error in form.username.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <input type="text" class="form-control" name="firstname" placeholder="First Name">
        {% for error in form.firstname.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <input type="text" class="form-control" name="middlename" placeholder="Middle Name">
        {% for error in form.middlename.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <input type="text" class="form-control" name="lastname" placeholder="Last Name">
        {% for error in form.lastname.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <div class="input-group">
            <label for="dob" class="col-form-label">Date Of Birth</label>&emsp;
            <input class="col-md-9 datepicker" id="dob" name="dob" type="date">
        </div>
        {% for error in form.dob.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <input type="text" class="form-control" name="email" placeholder="Email Address">
        {% for error in form.email.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <input type="password" class="form-control" name="password" placeholder="Password">
        {% for error in form.password.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        <input type="password" class="form-control" name="confirm_password" placeholder="Confirm Password">
        {% for error in form.confirm_password.errors %}
            <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="form-group">
        {{ form.submit(class="btn btn-block btn-outline-info") }}
    </div>
</form>

Heroku Application Log

2020-06-30T14:30:31.934555+00:00 app(web.1): Message: 'Exception on /register (POST)'
2020-06-30T14:30:31.934555+00:00 app(web.1): Arguments: ()
2020-06-30T14:30:31.943840+00:00 app(web.1): 10.43.184.146 - - (30/Jun/2020:14:30:31 +0000) "POST /register HTTP/1.1" 500 4924 "https://memoryblog.herokuapp.com/register" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
2020-06-30T14:30:31.951860+00:00 heroku(router): at=info method=POST path="/register" host=memoryblog.herokuapp.com request_id=31cb3cc1-c089-4b54-a35f-1a43050ef91a fwd="27.140.254.125" dyno=web.1 connect=1ms service=1558ms status=500 bytes=5290 protocol=https

Can anyone help me to solve this problem?