I am off a bad start on this pset, I understand the general idea but still having some difficulty, for register I want to return the user to apology("must provide username") for example, and I looked at the login function but my problem is that how to connect the button register to return this page if something went wrong because when I try the code below, if I didn't provide user name for example, it returns me back to the same register page and not to the apology("must provide username") page

Here is my function:

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

    return render_template("register.html")

    # forget any user_id

    # if user reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username")

        # ensure password was submitted
        elif not request.form.get("password"):
            return apology("must provide password")

        # ensure password confirmed
        elif not request.form.get("confirm password"):
            return apology("must confirm your password")

        # ensure passwords match
        elif request.form.get("confirm password") != request.form.get("password") :
            return apology("your passwords must match")

        return redirect(url_for("index"))

    # else if user reached route via GET (as by clicking a link or via redirect)
        return render_template("register.html")

and here is my register.html:

{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <form action="{{ url_for('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="confirm password" placeholder="confirm Password" type="password"/>
            <div class="form-group">
                <button class="btn btn-default" type="submit">register</button>
{% endblock %}

I just need someone to get me in the right path,, any help is appreciated,, thanks

1 Answer 1


Look at the beginning of your function:

    return render_template("register.html")

All code after that return is void, as return leaves the function. Your apologies will never be reached. Remove that line.

  • I tried to remove it as it did not make sense to me at first, but when I removed it I couldn't reach the register page(when I click register it goes directly to the first if and says you must provide username Commented Mar 16, 2017 at 13:52
  • I thought the return function works like what they say a decorator even though I don't really know what a decorator function is Commented Mar 16, 2017 at 13:54
  • return is not a function, return is a keyword telling the interpreter "end the function and return this to the calling code". When using decorators, they start with @
    – Blauelf
    Commented Mar 16, 2017 at 13:59

You must log in to answer this question.

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