1

I have implemented Pset-7 (Finance) but check50 shows an error called expected 400 but got 200 in all my apology pages and it is being rejected by check50. Since it successfuly landed to apology page, i think it is obvious to give 200 OK, Can anyone explain it to me? Here is screenshot of check50 terminal

My register.py:

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

def register():
    """Register user"""
    # checking if post method have been used
    if request.method == "POST":

        # checking if user has entered username
        if not request.form.get("username"):
            return apology("must provide username",400)

        # Ensure password was submitted
        if not request.form.get("password"):
            return apology("must provide password", 400)

        # Ensure confirmation was submitted
        if not request.form.get("confirmation"):
            return apology("must provide password twice", 400)

        # Ensure both password and confirmation are same
        if request.form.get("password") != request.form.get("confirmation"):
            return apology("passwords don't match", 400)

        # hashing password
        hash = generate_password_hash(request.form.get("password"))

        # adding username to database
        result = db.execute("INSERT INTO users (username,hash)  VALUES(:username, :hash)",
        username = request.form.get("username"), hash=hash )

        # if result is not successful then this means username already exists
        if not result:
            return apology("username already exists", 400)

        # storing id in session
        rows = db.execute("SELECT * FROM users WHERE username = :username ",
        username = request.form.get("username"))
        session["user_id"] = rows[0]["id"]

        # Redirect user to home page
        return redirect("/")


    else:
        #giving login page to users using get method
        return render_template("register.html")

My register.html {% extends "layout.html" %}

{% block title %}
    Register
{% 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>
        <div class="form-group">
            <input class="form-control" name="password" placeholder="Password" type="password">
        </div>
        <div class="form-group">
        <input class="form-control" name="confirmation" placeholder = "Password(again)" type="password">
        </div>
        <button class="btn btn-primary" type="submit">Register</button>
    </form>
{% endblock %}

1 Answer 1

1

I do not think the problem is in register function or register.html. I ran them through check50 and they passed. A little detail I missed on the initial read was "got 200 in all my apology pages", all being the operative. I can merely speculate, but it is possible that the older (2017) version of the application is in play. I believe you are using the 2017 version of the distro code except for the lookup function in helpers.py (to solve the yahoo deprecation problem). Sadly, I cannot test my theory with check50 because it is timing out. But when I run the 2017 app with only the lookup function replaced, apology returns a 200.

The function signatures between 2017 and 2018 differ, and I believe that explains the difference.

2017: def apology(top="", bottom=""):
2018: def apology(message, code=400):

To be clear, you need to use the 2018 apology function.


ORIGINAL RESPONSE

It can be argued that 200 is the valid response for calling apology successfully. The autograder sees it differently. Since apology is reporting the outcome of its caller, autograder expects 400 to indicate an error.

8
  • So how can I fix it?
    – R. Gurung
    Commented May 23, 2018 at 11:32
  • apology takes two arguments, the message and the error code. I'll have to assume that you sent 200 as the second argument. If you do not send an error code argument, it defaults to 400. So either send 400 or nothing so it defaults. Commented May 23, 2018 at 12:36
  • i am sending 400.
    – R. Gurung
    Commented May 23, 2018 at 12:42
  • # Ensure both password and confirmation are same elif request.form.get("password") != request.form.get("confirmation"): return apology("passwords don't match", 400)
    – R. Gurung
    Commented May 23, 2018 at 13:14
  • i have properly indented this.
    – R. Gurung
    Commented May 23, 2018 at 13:16

You must log in to answer this question.

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