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/extend_your_application/README.md
+11-8Lines changed: 11 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ The first thing we need in our blog is, obviously, a page to display one post, r
8
8
9
9
We already have a `Post` model, so we don't need to add anything to `models.py`.
10
10
11
-
## Create a link in the template
11
+
## Create a template link to a post's detail
12
12
13
13
We will start with adding a link inside `blog/templates/blog/post_list.html` file. So far it should look like:
14
14
```html
@@ -28,7 +28,7 @@ We will start with adding a link inside `blog/templates/blog/post_list.html` fil
28
28
29
29
```
30
30
31
-
{% raw %}We want to have a link to a post detail page on the post's title. Let's change `<h1><a href="">{{ post.title }}</a></h1>`into a link:{% endraw %}
31
+
{% raw %}We want to have a link from a post's title in the post list to the post's detail page. Let's change `<h1><a href="">{{ post.title }}</a></h1>`so that it links to the post's detail page:{% endraw %}
Let's create a URL in `urls.py` for our `post_detail`*view*!
46
48
47
-
### URL: http://127.0.0.1:8000/post/1/
49
+
We want our first post's detail to be displayed at this **URL**: http://127.0.0.1:8000/post/1/
48
50
49
-
We want to create a URL to point Django to a *view* named `post_detail` in the `blog/urls.py` file, that will show an entire blog post. Add the line `url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),` to the `blog/urls.py` file. It should look like this:
51
+
Let's make a URL in the `blog/urls.py` file to point Django to a *view* named `post_detail`, that will show an entire blog post. Add the line `url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),` to the `blog/urls.py` file. The file should look like this:
50
52
51
53
```python
52
54
from django.conf.urls import include, url
@@ -58,7 +60,7 @@ urlpatterns = [
58
60
]
59
61
```
60
62
61
-
That one looks scary, but no worries - we will explain it for you:
63
+
This part ``^post/(?P<pk>[0-9]+)/$`` looks scary, but no worries - we will explain it for you:
62
64
- it starts with `^` again -- "the beginning"
63
65
-`post/` only means that after the beginning, the URL should contain the word __post__ and __/__. So far so good.
64
66
-`(?P<pk>[0-9]+)` - this part is trickier. It means that Django will take everything that you place here and transfer it to a view as a variable called `pk`. `[0-9]` also tells us that it can only be a number, not a letter (so everything between 0 and 9). `+` means that there needs to be one or more digits there. So something like `http://127.0.0.1:8000/post//` is not valid, but `http://127.0.0.1:8000/post/1234567890/` is perfectly ok!
@@ -69,13 +71,13 @@ That means if you enter `http://127.0.0.1:8000/post/5/` into your browser, Djang
69
71
70
72
`pk` is shortcut for `primary key`. This name is often used in Django projects. But you can name your variable as you like (remember: lowercase and `_` instead of whitespaces!). For example instead of `(?P<pk>[0-9]+)` we could have variable `post_id`, so this bit would look like: `(?P<post_id>[0-9]+)`.
71
73
72
-
Ok! Let's refresh the page: http://127.0.0.1:8000/ Boom! Yet another error! As expected!
74
+
Ok, we've added a new URL pattern to `blog/urls.py`! Let's refresh the page: http://127.0.0.1:8000/ Boom! Yet another error! As expected!
73
75
74
76

75
77
76
78
Do you remember what the next step is? Of course: adding a view!
77
79
78
-
## post_detail view
80
+
## Add a post's detail view
79
81
80
82
This time our *view* is given an extra parameter `pk`. Our *view* needs to catch it, right? So we will define our function as `def post_detail(request, pk):`. Note that we need to use exactly the same name as the one we specified in urls (`pk`). Omitting this variable is incorrect and will result in an error!
81
83
@@ -115,6 +117,8 @@ It worked! But what happens when you click a link in blog post title?
115
117
116
118
Oh no! Another error! But we already know how to deal with it, right? We need to add a template!
117
119
120
+
## Create a template for post detail
121
+
118
122
We will create a file in `blog/templates/blog` called `post_detail.html`.
119
123
120
124
It will look like this:
@@ -145,7 +149,6 @@ Ok, we can refresh our page and see if `Page not found` is gone now.
145
149
146
150
Yay! It works!
147
151
148
-
149
152
## One more thing: deploy time!
150
153
151
154
It'd be good to see if your website will still be working on PythonAnywhere, right? Let's try deploying again.
0 commit comments