Skip to content

Commit 7eb9afc

Browse files
committed
Introduce named urls properly and remove deprecated urls constructs (fixes #171)
1 parent ea9e3fc commit 7eb9afc

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

en/django_forms/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ So once again we will create: a link to the page, a URL, a view and a template.
4444
It's time to open `blog/templates/blog/base.html`. We will add a link in `div` named `page-header`:
4545

4646
```html
47-
<a href="{% url 'blog.views.post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
47+
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
4848
```
4949

5050
Note that we want to call our new view `post_new`.
@@ -63,7 +63,7 @@ After adding the line, your html file should now look like this:
6363
</head>
6464
<body>
6565
<div class="page-header">
66-
<a href="{% url 'blog.views.post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
66+
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
6767
<h1><a href="/">Django Girls Blog</a></h1>
6868
</div>
6969
<div class="content container">
@@ -95,8 +95,8 @@ from django.conf.urls import include, url
9595
from . import views
9696

9797
urlpatterns = [
98-
url(r'^$', views.post_list),
99-
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
98+
url(r'^$', views.post_list, name='post_list'),
99+
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
100100
url(r'^post/new/$', views.post_new, name='post_new'),
101101
]
102102
```

en/django_models/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Let's open `blog/models.py`, remove everything from it and write code like this:
105105
from django.db import models
106106
from django.utils import timezone
107107

108+
108109
class Post(models.Model):
109110
author = models.ForeignKey('auth.User')
110111
title = models.CharField(max_length=200)
@@ -167,4 +168,4 @@ Django prepared for us a migration file that we have to apply now to our databas
167168
Rendering model states... DONE
168169
Applying blog.0001_initial... OK
169170

170-
Hurray! Our Post model is now in our database! It would be nice to see it, right? Jump to the next chapter to see what your Post looks like!
171+
Hurray! Our Post model is now in our database! It would be nice to see it, right? Jump to the next chapter to see what your Post looks like!

en/django_urls/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ After that, we can add our first URL pattern:
104104

105105
```python
106106
urlpatterns = [
107-
url(r'^$', views.post_list),
107+
url(r'^$', views.post_list, name='post_list'),
108108
]
109109
```
110110

111111
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.
112112

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+
113115
Everything all right? Open http://127.0.0.1:8000/ in your browser to see the result.
114116

115117
![Error](images/error1.png)

en/extend_your_application/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ We will start with adding a link inside `blog/templates/blog/post_list.html` fil
3131
{% 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 %}
3232

3333
```html
34-
<h1><a href="{% url 'blog.views.post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
34+
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
3535
```
3636

37-
{% 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 %}
3838

3939
`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*.
4040

@@ -46,15 +46,15 @@ Let's create a URL in `urls.py` for our `post_detail` *view*!
4646

4747
### URL: http://127.0.0.1:8000/post/1/
4848

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

5151
```python
5252
from django.conf.urls import include, url
5353
from . import views
5454

5555
urlpatterns = [
56-
url(r'^$', views.post_list),
57-
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
56+
url(r'^$', views.post_list, name='post_list'),
57+
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
5858
]
5959
```
6060

118 KB
Loading

0 commit comments

Comments
 (0)