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
Copy file name to clipboardExpand all lines: en/step_7.md
+17-15Lines changed: 17 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,24 @@
1
-
## Adding dynamic content
1
+
## Add dynamic content
2
2
3
-
So far you've learned to deliver static HTML web pages using templates. Lets add some dynamic content to the pages to display different information. Large websites like Facebook, YouTube and BBC News show different content depending on the page you visit, even though the templates are very similar.
3
+
Now you know how to deliver static HTML web pages using templates. Large websites like Facebook, YouTube and BBC News have dynamic content: these websites show different content depending on the page you visit, even though the templates are very similar.
4
4
5
-
Now you'll create a new route on your website so that when you go to `http://127.0.0.1/hello/name`, it will say "Hello name!" and replace 'name' with whatever you put there; so `/hello/Paul/` will display "Hello Paul!".
5
+
You will now add some dynamic content to your pages so they can display different information.
6
+
7
+
Now you will create a new route on your website so that when you go to `http://127.0.0.1/hello/name`, the page says 'Hello name!', replacing 'name' with whatever you put there. So for example, `/hello/Dana/` displays 'Hello Dana!'.
6
8
7
9
--- task ---
8
10
9
-
Create a new route in your application like so:
11
+
Add the following code to create a new route in your application:
10
12
11
13
```python
12
14
@app.route('/hello/<name>')
13
15
defhello(name):
14
16
return render_template('page.html', name=name)
15
17
```
16
18
17
-
-`@app.route('/hello/<name>')` - the `<name>` part means it passes the name into the `hello` function as a variable called `name`
18
-
-`def hello(name)` - this is the function that determines what content is shown - this time it takes the given name as a parameter
19
-
-`return render_template('page.html', name=name)` - here we look up the template `page.html` and pass in the variable `name` from the URL, so the template can use it
19
+
-`@app.route('/hello/<name>')`: the `<name>` part means it passes the name into the `hello` function as a variable called `name`.
20
+
-`def hello(name)`: this is the function that determines what content is shown. Here, the function takes the given name as a parameter.
21
+
-`return render_template('page.html', name=name)`: this code looks up the template `page.html` and passes in the variable `name` from the URL so that the template can use it.
20
22
21
23
--- /task ---
22
24
@@ -36,11 +38,11 @@ Create a new HTML template called `page.html`, and add the following HTML code t
36
38
37
39
--- task ---
38
40
39
-
Save the files and visit `http://127.0.0.1:5000/hello/paul`. It should look like this:
41
+
Save the files and visit `http://127.0.0.1:5000/hello/paul`. The page you see should look like this:
40
42
41
43

42
44
43
-
Try it with different names!
45
+
Try `http://127.0.0.1/hello/name` with different names!
44
46
45
47
--- /task ---
46
48
@@ -50,23 +52,23 @@ Try it with different names!
50
52
title: What's happening here?
51
53
---
52
54
53
-
Flask uses `jinja`, a Python library for rendering templates. Using the braces (curly brackets) on this line:
55
+
Flask uses `jinja`, a Python library for rendering templates. Look at this code with the braces (curly brackets):
54
56
55
57
```html
56
58
<h1>Hello {{ name }}!</h1>
57
59
```
58
60
59
-
... tells the template to render the variable `name`which was passed in the route function `hello`.
61
+
This code tells the template to render the variable `name`that was passed in the route function `hello`.
60
62
61
-
Visiting `127.0.0.1:5000/hello/` without a name will create an error. Think about how you can prevent this from happening.
63
+
Visiting `127.0.0.1:5000/hello/` without a name creates an error. Try to think of a way to prevent this error.
62
64
63
65
--- /collapse ---
64
66
65
67
--- task ---
66
68
67
-
Create a link to your new dynamic hello page from your index.
69
+
Create a link to your new, dynamic hello page from your index.
68
70
69
-
Edit `index.html` to add a link to your new hello page under the heading.
71
+
Edit `index.html` to add a link to the hello page under the heading.
70
72
71
73
```html
72
74
<h1>My website</h1>
@@ -77,7 +79,7 @@ Edit `index.html` to add a link to your new hello page under the heading.
77
79
78
80
--- task ---
79
81
80
-
Save your changes and refresh the index page to see the result.
82
+
Save the changes to `index.html`, and then refresh the index page in the browser to see the updated version.
0 commit comments