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/django_urls/README.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,12 +104,14 @@ After that, we can add our first URL pattern:
104
104
105
105
```python
106
106
urlpatterns = [
107
-
url(r'^$', views.post_list),
107
+
url(r'^$', views.post_list, name='post_list'),
108
108
]
109
109
```
110
110
111
111
As you can see, we're now assigning a `view` called `post_list` to `^$` URL. This regular expression will match `^` (a beginning) followed by `$` (an end) - so only an empty string will match. And that's correct, because in Django url resolvers, 'http://127.0.0.1:8000/' is not a part of URL. This pattern will show Django that `views.post_list` is the right place to go if someone enters your website at the 'http://127.0.0.1:8000/' address.
112
112
113
+
The last part `name='post_list'` is the name of the url that will be used to identify the view. This can be the same as the name of the view but it can also be something completelly different. We will be using the named urls later in the project but it is important to name each url in the app and to try to keep the names of urls unique and easy to remember.
114
+
113
115
Everything all right? Open http://127.0.0.1:8000/ in your browser to see the result.
Copy file name to clipboardExpand all lines: en/extend_your_application/README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,10 +31,10 @@ We will start with adding a link inside `blog/templates/blog/post_list.html` fil
31
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 %}
{% raw %}Time to explain the mysterious `{% url 'blog.views.post_detail' pk=post.pk %}`. As you might suspect, the `{% %}` notation means that we are using Django template tags. This time we will use one that will create a URL for us!{% endraw %}
37
+
{% raw %}Time to explain the mysterious `{% url 'post_detail' pk=post.pk %}`. As you might suspect, the `{% %}` notation means that we are using Django template tags. This time we will use one that will create a URL for us!{% endraw %}
38
38
39
39
`blog.views.post_detail` is a path to a `post_detail`*view* we want to create. Please note: `blog` is the name of our application (the directory `blog`), `views` is from the name of the `views.py` file and the last bit - `post_detail` - is the name of the *view*.
40
40
@@ -46,15 +46,15 @@ Let's create a URL in `urls.py` for our `post_detail` *view*!
46
46
47
47
### URL: http://127.0.0.1:8000/post/1/
48
48
49
-
We want to create a URL to point Django to a *view*called`post_detail`, that will show an entire blog post. Add the line `url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),` to the `blog/urls.py` file. It should look like this:
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:
0 commit comments