Exercise

Implement a flask app to look up a staff member's UID, given their name.

  1. Copy the lecture2.py file to staff_lookup_app.py.
  2. Provide a route to display a staff member's name and UID.
  3. Provide a form to request the name and a route to process the request.
  4. redirect to the page for the staff member.
  5. Flash a message if the name isn't found.
  6. If you're having trouble getting started, talk to me.

Login Page

We can combine redirects and flashing to implement a login page in Flask, modeled after the example in the reading.

  1. Copy the flaskStarter directory to your account (outside public_html)
  2. Get it working
  3. Modify it to provide a login form
  4. Modify it to process the logins. Don't use a database; just have a Python dictionary of usernames and passwords (see below)
  5. A successful login should go to /user/fred where fred is the person logged in. The page should also say that somewhere.
  6. An unsuccessful login should return to the login form and tell them they failed.

You might do the login database like this:

passwds = {'fred': 'stinkbomb',
           'george': 'butterbeer'}

Here's my solution. It's just 25 lines of code.


@app.route('/login/', methods=["GET", "POST"])
def login():
    if request.method == 'GET':
        return render_template('login-form.html')
    # Dealing with POST
    passwds = {'fred': 'stinkbomb',
               'george': 'butterbeer'}
    try:
        user = request.form['username']
        passwd = request.form['password']
        if passwds[user] == passwd:
            flash("You're logged in")
            return redirect( url_for('user', username=user ))
        else:
            flash("Sorry; incorrect")
            return render_template('login-form.html')
    except Exception as err:
        print 'Got this in /login/: ',err
        flash("Sorry, some kind of error occurred")
        return render_template('login-form.html')

@app.route('/user/')
def user(username):
    return render_template('userpage.html', username=username)