Skip to content

Commit

Permalink
created step-10
Browse files Browse the repository at this point in the history
  • Loading branch information
fromzeroedu committed Jan 9, 2015
1 parent c90c8e3 commit 93712b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### Step #9
### Step #10

A better login page using request.method and request.form (A MultiDict with the parsed form data from POST or PUT requests), whereas request.values combines both form and args.
Now we write a simple function that checks for the right password and if not return an error context. We're also now using request.form.get()

Also changed the HTML field to password.
15 changes: 13 additions & 2 deletions hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@

@app.route('/login', methods=['POST', 'GET'])
def login():
error = None
if request.method == 'POST':
return "User %s logged in" % request.form['username']
return render_template('login.html')
if valid_login(request.form.get('username'),
request.form.get('password')):
return "Welcome back, %s" % request.form.get('username')
else:
error = "Incorrect username and password"
return render_template('login.html', error=error)

def valid_login(username, password):
if username == password:
return True
else:
return False

if __name__ == '__main__':
app.debug = True
Expand Down
9 changes: 7 additions & 2 deletions templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@

<form action="/login" method="POST">

{% if error %}
<p style="color:red;">
{{ error }}
</p>
{% endif %}

<p>
Username: <input type="text" name="username" />
</p>
<p>
Password: <input type="text" name="password" />
Password: <input type="password" name="password" />
</p>

<button type="Submit">Login</button>


</form>

0 comments on commit 93712b2

Please sign in to comment.