@@ -9,7 +9,7 @@ a page, all of those comments being stored in a database:
9
9
10
10
<img width =" 500 " src =" /static/images/flask-tutorial-final-result.png " >
11
11
12
- It was made up of two files; a Python one which controlled what it did, and a template one that controlled
12
+ It was made up of two files; a Python file which controlled what it did, and a template file that controlled
13
13
how it was displayed. All of our code was under source-code control, and it was running as a website on
14
14
PythonAnywhere.
15
15
@@ -26,19 +26,19 @@ IMAGE HERE -- login page
26
26
IMAGE HERE -- main page
27
27
28
28
Adding these new features will require us to do a bit of work in the background; our existing site works
29
- fine for what it is, but it's not very extensible, so we'll fix that as we go. You'll learn about the
30
- Flask-Login extension, database migrations, and virtualenvs -- all very useful stuff :-)
29
+ fine for what it is, but it's not very extensible, and we'll fix that as we go. You'll learn about the
30
+ Flask-Login extension, database migrations, and virtualenvs -- all very useful stuff.
31
31
32
32
Let's get started!
33
33
34
34
35
- ## Handling login and logout
35
+ ## Handling login and logout: the basics
36
36
37
- Just like we did before, we'll start off by designing how the site should work . We already have a
37
+ Just like we did before, we'll start off by designing how the site should look . We already have a
38
38
page that lists all of the comments, and allows people to add new comments. But we don't have a login page,
39
39
so let's start by writing one. Log in to PythonAnywhere, then go to the "Files" tab, and then down into the "mysite"
40
40
directory that contains your code, and then to the "templates" subdirectory. Enter the filename "login_page.html"
41
- into the "new file" input, then click the "new file button" . You'll find yourself in the (hopefully very familiar) editor,
41
+ into the "new file" input, then click the "new file" button. You'll find yourself in the (hopefully very familiar) editor,
42
42
where you should enter the following HTML template code:
43
43
44
44
<html>
@@ -88,7 +88,8 @@ where you should enter the following HTML template code:
88
88
</body>
89
89
</html>
90
90
91
- This should be pretty clear, certainly if you're familiar with HTML -- most of it is boilerplate, and the form inside
91
+ This should be pretty clear -- like the template from the first part of the tutorial, most of it is boilerplate to make it look similar to the
92
+ existing front page, and the form inside
92
93
the row (inside the container inside the body)
93
94
is a simple login form with username and password fields, each of which has a label, and a "Log in" button to submit the form.
94
95
Note that the ` type ` of the password field is "password" --
@@ -104,29 +105,36 @@ near the top of the page, and select the option to open it in a new browser tab.
104
105
def login():
105
106
return render_template("login_page.html")
106
107
107
- Reload the app the button at the top right of the page -- in case you've forgotten, it looks like this:
108
+ Save the file, then reload the website using the button at the top right of the page -- in case you've forgotten, it looks like this:
108
109
109
- IMAGE HERE
110
+ < img src = " /static/images/flask-tutorial-reld-button.png " >
110
111
111
112
Once that's done, go to your site in yet another browser tab -- remember, it's at * yourpythonanywhereusername* `.pythonanywhere.com.
112
113
You should now have three tabs open -- the template, the Python code, and your site.
113
114
114
115
On your site, you'll see what you did before -- the page with the list of comments. So next, edit the URL in the browser's address bar
115
116
and add "/login/" to the end. Hit return, and we get the login page:
116
117
117
- IMAGE HERE
118
+ < img src = " /static/images/flask-tutorial-login-page-initial.png " >
118
119
119
120
So far, so good! But if you enter a username and a password into the form, you'll get a "Method not allowed" error:
120
121
121
- IMAGE HERE
122
+ < img width = " 500 " src = " /static/images/flask-tutorial-method-not-allowed.png " >
122
123
123
124
Hopefully that will be
124
125
familiar from the first tutorial; our form is sending a ` POST ` request when the "Log in" button is clicked, and our
125
126
view doesn't handle that method yet.
126
127
127
128
We've reached a state where our site is doing something new, so let's use git to take a checkpoint. In yet another new
128
- browser tab, open up a Bash console from the PythonAnywhere "Consoles" tab. Change your working directory
129
- to ` mysite ` , which is where all of our code is, by running this command:
129
+ browser tab, open up a new Bash console from the PythonAnywhere "Consoles" tab.
130
+
131
+ <img width =" 500 " src =" /static/images/flask-tutorial-new-console-links.png " >
132
+
133
+ (If you're using a free account and you
134
+ have two consoles open from going through the tutorial previously, won't be able to open a new one, so you should close
135
+ the old ones using the "X"s next to their names, so that you can start with a completely fresh console.)
136
+
137
+ In the new console, change your working directory to ` mysite ` , which is where all of our code is, by running this command:
130
138
131
139
cd mysite
132
140
0 commit comments