-1

I'm trying to learn about login/password/user session stuff in flask. i found this link and have been trying to understand the code it provides (on the bottom of the page, the largest piece of code).

http://thecircuitnerd.com/flask-login-tokens/

The link doesn't provide, though, the contents of the login.html file.

So far, the way i've been handling forms in flask requires me to specify to the render_template function what user input will be attributed to each python variable. But since the author didn't do it, i suppose his method of getting the user input should be different than that.

2
  • It's not really clear what you're asking - are you wondering how the /login route handler gets username and password? Mar 30, 2016 at 1:43
  • "It's not really clear what you're asking - are you wondering how the /login route handler gets username and password?" that's right @SeanVieira Mar 30, 2016 at 2:05

2 Answers 2

2

If you look at the login route handler in the code you linked you'll see that it uses request.form to get out two variables, 'username' and 'password':

@app.route("/login/", methods=["GET", "POST"])
def login_page():
    """
    Web Page to Display Login Form and process form. 
    """
    if request.method == "POST":
        user = User.get(request.form['username'])

        #If we found a user based on username then compare that the submitted
        #password matches the password in the database.  The password is stored
        #is a slated hash format, so you must hash the password before comparing
        #it.
        if user and hash_pass(request.form['password']) == user.password:
            login_user(user, remember=True)
            return redirect(request.args.get("next") or "/")        

    return render_template("login.html")

The simplest way to do this would be with the following HTML:

<form action="/login/" method="POST">
  <input name="username" placeholder="username">
  <input type="password" name="password" placeholder="password">
  <input type="submit" value="Login">
</form>

This will not re-populate the username if the user mis-types their username or password, nor will it give the user any indication that they failed to login. They will just see the login form again. However, this is just some example code, so it's understandable that the author chose to leave out useful code that would obscure the point he was trying to make.

1
  • do you have any links that will help me to understand these mechanisms better? thank you @seanvieira Mar 30, 2016 at 3:08
0
 <form action="/login/" method="POST">
      <input name="username" placeholder="username">
      <input type="password" name="password" placeholder="password">
      <input type="submit" value="Login">
    </form>
<?PHP
@app.route("/login/", methods=["GET", "POST"])
def login_page():
    """
    Web Page to Display Login Form and process form. 
    """
    if request.method == "POST":
        user = User.get(request.form['username'])

        #If we found a user based on username then compare that the submitted
        #password matches the password in the database.  The password is stored
        #is a slated hash format, so you must hash the password before comparing
        #it.
        if user and hash_pass(request.form['password']) == user.password:
login_user(user, remember=True)
return redirect(request.args.get("next") or "/")        

    return render_template("login.html")
?>
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 9, 2023 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.