Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit ee2a8b3

Browse files
committed
Add content for sessions
TODO: Need to add extensions-   use post button   add simple admin function
1 parent e169a47 commit ee2a8b3

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

extras/sessions.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,64 @@ layout: ots
33
title: Keeping track of users
44
---
55

6-
How to use flask sessions to remember the list of emails in a session
6+
## What are sessions, and why do we need them?
7+
8+
HTTP is, by itself, a _stateless_ protocol. That means it doesn't keep track of information or change much in-between requests. You should see the conundrum implicated by this if you look at the fact that practically every application on the web _does_ keep track of information - namely, they mostly keep track of you. If facebook was purely stateless, it wouldn't be able to recognise that you were logged in from one page to another - and probably wouldn't be very popular.
9+
10+
A session then, is simply the name web developers usually give to the mechanism that lets you do this when serving a web application.
11+
12+
If you'd like to know how it works, the web server is usually instructed to send your browser a [HTTP cookie](http://en.wikipedia.org/wiki/HTTP_cookie_) along with the page content, when visit. The cookie holds a unique value - that your browser will send back to the server with every request - that the application can then look up and re-identify that you are viewing a page again.
13+
14+
In case you were wondering, when we run flask, it is acting as both a web server _and_ the web application (nicely bundled together to be easy to use).
15+
16+
## So flask has sessions, right? How does one use them?
17+
18+
It sure does, and they're very simple to use. Firstly, **add `session` as another module to import** from the `flask` package:
19+
20+
from flask import session
21+
22+
Then you need to give flask a secret key to use, to make sure the session is secure. To get a nice secret key, run this at a command prompt:
23+
24+
python -c "import os; print repr(os.urandom(24))"
25+
26+
Just copy this string to use. To set this as the flask app's secret key, just **add the following line** after you initialize your `app` (use your own key!):
27+
28+
app.secret_key = '#d\xe9X\x00\xbe~Uq\xebX\xae\x81\x1fs\t\xb4\x99\xa3\x87\xe6.\xd1_'
29+
30+
You can now use the `session` variable just as you would a dictionary, anywhere in your application. It will be a special dictionary, in that any key/value you set will be specific only for the user you set it to. When a different user visits your server, it will hold their own value (or not be there).
31+
32+
How can we use this? Let's say we would like users to be able to delete their own email off of the email list.
33+
34+
First, we have to **modify the `signup()` route** to remember the user's email address in the session:
35+
36+
@app.route('/signup.html', methods = ['POST'])
37+
def signup():
38+
email = request.form['email']
39+
email_addresses.append(email)
40+
session['email'] = email
41+
print(email_addresses)
42+
return redirect('/')
43+
44+
You can see that we simply added a line to save that email to our session dictionary.
45+
46+
Then we can **create a new route**, that the user can visit to delete their address.
47+
48+
@app.route('/unregister')
49+
def unregister():
50+
# Make sure they've already registered an email address
51+
if 'email' not in session:
52+
return "You haven't submitted an email!"
53+
email = session['email']
54+
# Make sure it was already in our address list
55+
if email not in email_addresses:
56+
return "That address isn't on our list"
57+
email_addresses.remove(email)
58+
del session['email'] # Make sure to remove it from the session
59+
return 'We have removed ' + email + ' from the list!'
60+
61+
62+
Here, we use a few `if` checks to make sure that our user added an email address already, and it's in our list. We can't remove it otherwise!
63+
64+
**Try out the new functionality!** Add an email address, and then visit `/unregister` in your web browser. When you next look at the list, it should hopefully be gone!
65+
66+
### Extensions

0 commit comments

Comments
 (0)