When I try to register, I get a 500 internal serve error message.

I looked at my terminal and it shows this: enter image description here

I am not sure how I can make sense of this... I'm thinking that the problem at is line 201? Appreciate if someone can help me understand what flask is saying...

I also did a check50 and it seems that this is the error "exception raised in application: AttributeError: 'Request' object has no attribute 'get' ". I'm not sure what this means, but does it mean that my form is accepting people who are getting to my page via GET? Here is the link to the full check50: https://cs50.me/checks/58e73e13f8399a0b97915ac8b77554b278027be2

Here is my code for application.py:

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

    #first, check if user reached page via POST
    if request.method == "POST":

        #check if username is filled in
        if not request.form.get("username"):
            return apology("Please provide a username")

        #check if password is filled in
        elif not request.form.get("password"):
            return apology("Please provide a password")

        #check if passwords match
        # use != for "not equal"
        elif request.form.get("password") != request.form.get("confirmation"):
            return apology("Please ensure the passwords matches")

        #store user's password
        #protect the password by storing a hash of that password

        #add user details to our database via INSERT query
        #INSERT query will allow us to insert into a particular table
        #check if username has been taken
        result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.get.form("username"), hash=pwd_context.encrypt(request.form.get("password")))
        if not result:
            return apology("Username has been taken")

        # log users in
        session["user_id"] = result

        # redirect users to homepage after logging them in
        return redirect("/")

    # else:if user get to page via GET
        return render_template("register.html")

and here is my code for register.html

    {% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <form action="/register" method="post">
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text">
        <div class="form-group">
            <input class="form-control" name="password" placeholder="Password" type="password">
        <div class="form-group">
            <input class="form-control" name="confirmation" placeholder="Re-enter Password" type="password">
        <button class="btn btn-primary" type="submit">Register</button>
{% endblock %}

Thank you so much


A simple typo! Double-check the line db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.get.form("username"), hash=pwd_context.encrypt(request.form.get("password"))) particularly username=request.get.form("username"). It should be username=request.form.get("username").

  • hello! thanks for pointing this typo out. after fixing it, i still had the same error message but check50 changed and gave a hint, and I managed to solve some bits of it! Turns out pwd_context.encrypt was outdated (even though it was in the walkthroughs!) and we should be using generate_password_hash instead. Thank you :)
    – freshwound
    Apr 15 '19 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .