You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 19, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: core/data.md
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -131,7 +131,7 @@ Outside of Python, dictionaries are often called ``hash tables``, ``hash maps``
131
131
132
132
At the moment we don't have any way to store email addresses. But if we add a list to our website then we can keep them temporarily. Sure it will only stay around until the web server stops, but it is a good start.
133
133
134
-
Let's add a list to the top of the Python code, just after all the `import` lines and `app = Flask(__name__)`:
134
+
Let's add an empty list to the top of the Python code, just after all the `import` lines and `app = Flask(__name__)`:
135
135
136
136
email_addresses = []
137
137
@@ -141,26 +141,26 @@ Now in our `signup()` function we can add the email address instead of printing
141
141
def signup():
142
142
email = request.form['email']
143
143
email_addresses.append(email)
144
-
print(email_addresses)
144
+
print(email_addresses)
145
145
return redirect('/')
146
146
147
-
Check the output you get when submitting a form. It prints out the entire list every time.
147
+
Check the output you get in the console, when submitting a form. It prints out the entire list every time.
148
148
149
149
## Listing email addresses
150
150
151
-
Printing out our email addresses to the console is a bit of a hassle. If you wanted to read the list then you have to open the terminal and scan through for the printout. It would be so much easier to have a web page that lists all the email addresses that we have collected.
151
+
Just printing out our email addresses is a bit of a hassle. If you wanted to read the list then you have to open the terminal and scan through for the printout. It would be so much easier to have a web page that lists all the email addresses that we have collected.
152
152
153
-
We know how to create a new page in Flask, it's just a function. And we can pass the list of emails directly to our HTML template like so:
153
+
We know how to create a new page in Flask, it's just a function. And we can pass the list of emails directly to a HTML template like so:
Here the `render_template()` function takes the name of the template (`'emails.html'`) and extra data to be used when rendering the page. We wrote `email_addresses = email_addresses` to say that our variable `email_addresses` will be available in the template using the same name.
159
+
Here the `render_template()` function takes the name of the template (`'emails.html'`) and extra data to be used when rendering the page. We wrote `email_addresses=email_addresses` to say that our variable `email_addresses` will be available in the template using the same name.
160
160
161
161
Now we need our HTML template. In our previous template we didn't actually do anything but plain HTML. But now we need to print the list of email addresses, so we'll use a `for` loop in the template.
162
162
163
-
Here is the content of `emails.html` for the _templates_ directory:
163
+
Here is an example of the content of `emails.html` for the _templates_ directory:
164
164
165
165
{% raw %}
166
166
@@ -182,7 +182,9 @@ Here is the content of `emails.html` for the _templates_ directory:
182
182
183
183
{% endraw %}
184
184
185
-
Check out the _for_ loop that we use in the template. It is similar to the one used in Python but subtly different. You can see it doesn't contain the `:` at the end of the first line, and it needs an `endfor` statement to end the loop.
185
+
Check out the _for_ loop that we use in the template. It is similar to the one used in Python but subtly different. You can see it doesn't contain the `:` at the end of the first line, and it needs an `endfor` statement to end the loop. This comes from the template making use of flask's Jinja2 templating language, that's designed to be very simple to read and write.
186
+
187
+
Now you can **reboot your server, submit some emails, and visit [/emails.html](http://127.0.0.1:5000/emails.html)** to check everything is working as expected! What happens if you restart the server again?
0 commit comments