0

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
    session.clear()

    # 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)
    else:
        return render_template("register.html")

and here is my register.html:

{% extends "layout.html" %}

{% block title %}
    Register
{% endblock %}

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

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

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 – Yasser Altamimi Mar 16 '17 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 – Yasser Altamimi Mar 16 '17 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 Mar 16 '17 at 13:59

You must log in to answer this question.

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