Skip to content

Commit f2bf7b0

Browse files
committed
Merge pull request #459 from willingc/patch-1
Suggest user friendly edits for Extend chapter
2 parents 77c8ce2 + 9f3e941 commit f2bf7b0

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

en/extend_your_application/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The first thing we need in our blog is, obviously, a page to display one post, r
88

99
We already have a `Post` model, so we don't need to add anything to `models.py`.
1010

11-
## Create a link in the template
11+
## Create a template link to a post's detail
1212

1313
We will start with adding a link inside `blog/templates/blog/post_list.html` file. So far it should look like:
1414
```html
@@ -28,7 +28,7 @@ We will start with adding a link inside `blog/templates/blog/post_list.html` fil
2828

2929
```
3030

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 %}
3232

3333
```html
3434
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
@@ -42,11 +42,13 @@ Now when we go to: http://127.0.0.1:8000/ we will have an error (as expected, si
4242

4343
![NoReverseMatch error](images/no_reverse_match2.png)
4444

45+
## Create a URL to a post's detail
46+
4547
Let's create a URL in `urls.py` for our `post_detail` *view*!
4648

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/
4850

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:
5052

5153
```python
5254
from django.conf.urls import include, url
@@ -58,7 +60,7 @@ urlpatterns = [
5860
]
5961
```
6062

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:
6264
- it starts with `^` again -- "the beginning"
6365
- `post/` only means that after the beginning, the URL should contain the word __post__ and __/__. So far so good.
6466
- `(?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
6971

7072
`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]+)`.
7173

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!
7375

7476
![AttributeError](images/attribute_error2.png)
7577

7678
Do you remember what the next step is? Of course: adding a view!
7779

78-
## post_detail view
80+
## Add a post's detail view
7981

8082
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!
8183

@@ -115,6 +117,8 @@ It worked! But what happens when you click a link in blog post title?
115117

116118
Oh no! Another error! But we already know how to deal with it, right? We need to add a template!
117119

120+
## Create a template for post detail
121+
118122
We will create a file in `blog/templates/blog` called `post_detail.html`.
119123

120124
It will look like this:
@@ -145,7 +149,6 @@ Ok, we can refresh our page and see if `Page not found` is gone now.
145149

146150
Yay! It works!
147151

148-
149152
## One more thing: deploy time!
150153

151154
It'd be good to see if your website will still be working on PythonAnywhere, right? Let's try deploying again.

0 commit comments

Comments
 (0)