###JavaScript
JavaScript
$.get
is asynchronous. It triggers an HTTP request and returns. The code meant to prevent submission runs after submission is processed, and therefore can't prevent it.
The preventDefault
has to go into the submit
handler, not the $.get
callback, and would be called each time you try to submit, cancelling the submit attempt. Inside the get callback, depending on result, call the form's submit()
method, that one submits the form without triggering the submit handlers again.
###Python
Python
SELECT
lets db.execute
return a list of dicts, so [0]
would pick first record, and [username]
would pick the field of the name stored in variable username
. Which is not what you want, you want to compare with values. Instead of [0][username]
, you could do something like
usernames = (user["username"] for user in db.execute("SELECT username FROM users"))
And you can jsonify
a boolean expression:
return jsonify(username in usernames)
edit: username not in usernames (according to problem description)
You might like WHERE
clause in your SELECT
. Gets so much more readable, saving you much code.