0

I keep getting the same errors. I'm pretty sure the reason most my code won't work is because of my check function. I don't if it's not communicating well with my register function (which I am entirely sure is correct).  FI

Below is my code for check:

@app.route("/check", methods=["GET"])
def check():
    """Return true if username available, else false, in JSON format"""

    username = request.args.get("user")

    names = db.execute("SELECT username FROM users WHERE username = :username", username=username)

    if names and username:
        return jsonify(False)
    elif not names and username:
        return jsonify(True)
    else:
        return jsonify(False)

    return render_template("register.html")

The following is my register function:

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


    #if the user reached the route via POST
    if request.method == "POST":

        #Make sure user submits username
        if not request.form.get("username"):
            return apology("Provide Username", 400)

        #Make sure user submits password
        elif not request.form.get("password"):
            return apology("Provide Password", 400)

        #Make sure user submits confirmation password
        elif not request.form.get("confirmation"):
            return apology("Must provide confirmation", 400)

        #Make sure user submits a password that matches
        elif request.form.get("password") != request.form.get("confirmation"):
            return apology("Passwords do not match", 400)

        #Hash the password
        #Add user into the database
        newUsers = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username=request.form.get("username"), hash = generate_password_hash(request.form.get("password")))


        if not newUsers:
            return apology("The username is already taken")

        rows = db.execute("SELECT * FROM users WHERE username = :username",
                  username = request.form.get("username"))


        # Remember which user has logged in
        session["user_id"] = rows[0]["id"]

        #Display a flash message
        flash("You are Registered!")

        # Redirect user to home page
        return redirect(url_for("index"))

    #if user reached the route via GET
    else:
        return render_template("register.html")

The following is my register.html code:

{% extends "layout.html" %}

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

{% block main %}
<script>
     $('document').ready(function(){
        $('form').on('submit', function(e){
            e.preventDefault();
            var user = $("#username").val();
            $.get('/check?username'+ user, function(response){
                if(response == false){
                    alert('Username is already taken!')
                    document.getElementById('register').reset();
                    $('#username').focus();
                }
                else{
                    (document).getElementById('register').submit();
                }
            });
        });
    });
</script>
    <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 %}

Someone please help me. This is issue is driving me crazy and I feel like I'm so close but without check working properly the rest of my code won't work as well, which by the way I'm sure was implemented correctly.

2
  • Does your users table have any fields other than username and hash that are neither an integer primary key (which automatically has some sort of autoincrement), nor have a default value? Also, what happens on your own machine, are there any error messages on the terminal where you flask run your app?
    – Blauelf
    Oct 1 '19 at 16:58
  • Asking because I'm not even sure the checker will call /check before it tries to register the first user.
    – Blauelf
    Oct 1 '19 at 17:05
0

One mistake in the code calling /check:

$.get('/check?username'+ user, should be $.get('/check?username=' + user,, even better $.get('/check?username=' + encodeURIComponent(user),, or $.get('/check', {username: user}, (the last one passing the task of encoding to jQuery).

And one in /check itself:

username = request.args.get("user") should be username = request.args.get("username")

3
  • I changed my check function to username = request.args.get("username") also changed my register.html to $.get('/check?username=' + user and I'm still coming up with the same issues.
    – user24000
    Oct 1 '19 at 23:23
  • But what happens if you execute in your VM? check50 should be run when you are pretty confident in your solution already. Are there any stack traces on the terminal? If so, edit them into your question.
    – Blauelf
    Oct 2 '19 at 5:21
  • It doesn't execute. I can't load the site, It updates my rows though.
    – user24000
    Oct 2 '19 at 19:59
0

I'm having the exact same issues with my code. My code looks extremely similar and every time I run check50 I get the same three errors. When I click on the check50 link I get the following for all three errors:

:( registering user succeeds application raised an exception (rerun with --log for more details) Log sending POST request to /register exception raised in application: TemplateSyntaxError: expected token ',', got 'string'

No matter how I change my register.html or application.py nothing works. I've checked all the spelling is correct etc. Is this the error you are getting as well? Does anyone know how to fix this?

5
  • Yeah I am getting the exact same error. I really don't know what I'm missing
    – user24000
    Oct 2 '19 at 19:58
  • I just found my issue with some help on the question about this that I posted. My issue was in my portfolio.html (which is what I called the portfolio portion). I needed to change some of my syntax and it fixed it. Take a look at your portfolio.html for any missed quotes or syntax errors and it may help.
    – Cori
    Oct 2 '19 at 23:40
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – MARS
    Oct 3 '19 at 21:55
  • Wait, portfolio.html? I don't think I have that template.
    – user24000
    Oct 9 '19 at 1:01
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
    – Cliff B
    Nov 8 '19 at 23:28

You must log in to answer this question.