Skip to content

Commit fc21432

Browse files
chrisshyitomchristie
authored andcommitted
Update tutorial to Django 2.0 routing syntax (#5963) (#5964)
Update tutorial to Django 2.0 routing syntax
1 parent 4340ff4 commit fc21432

7 files changed

+31
-31
lines changed

docs/tutorial/1-serialization.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,20 @@ We'll also need a view which corresponds to an individual snippet, and can be us
275275

276276
Finally we need to wire these views up. Create the `snippets/urls.py` file:
277277

278-
from django.conf.urls import url
278+
from django.urls import path
279279
from snippets import views
280280

281281
urlpatterns = [
282-
url(r'^snippets/$', views.snippet_list),
283-
url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
282+
path('snippets/', views.snippet_list),
283+
path('snippets/<int:pk>/', views.snippet_detail),
284284
]
285285

286286
We also need to wire up the root urlconf, in the `tutorial/urls.py` file, to include our snippet app's URLs.
287287

288-
from django.conf.urls import url, include
288+
from django.urls import path, include
289289

290290
urlpatterns = [
291-
url(r'^', include('snippets.urls')),
291+
path('', include('snippets.urls')),
292292
]
293293

294294
It's worth noting that there are a couple of edge cases we're not dealing with properly at the moment. If we send malformed `json`, or if a request is made with a method that the view doesn't handle, then we'll end up with a 500 "server error" response. Still, this'll do for now.

docs/tutorial/2-requests-and-responses.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ and
108108

109109
Now update the `snippets/urls.py` file slightly, to append a set of `format_suffix_patterns` in addition to the existing URLs.
110110

111-
from django.conf.urls import url
111+
from django.urls import path
112112
from rest_framework.urlpatterns import format_suffix_patterns
113113
from snippets import views
114114

115115
urlpatterns = [
116-
url(r'^snippets/$', views.snippet_list),
117-
url(r'^snippets/(?P<pk>[0-9]+)$', views.snippet_detail),
116+
path('snippets/', views.snippet_list),
117+
path('snippets/<int:pk>', views.snippet_detail),
118118
]
119119

120120
urlpatterns = format_suffix_patterns(urlpatterns)

docs/tutorial/3-class-based-views.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ That's looking good. Again, it's still pretty similar to the function based vie
6464

6565
We'll also need to refactor our `snippets/urls.py` slightly now that we're using class-based views.
6666

67-
from django.conf.urls import url
67+
from django.urls import path
6868
from rest_framework.urlpatterns import format_suffix_patterns
6969
from snippets import views
7070

7171
urlpatterns = [
72-
url(r'^snippets/$', views.SnippetList.as_view()),
73-
url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()),
72+
path('snippets/', views.SnippetList.as_view()),
73+
path('snippets/<int:pk>/', views.SnippetDetail.as_view()),
7474
]
7575

7676
urlpatterns = format_suffix_patterns(urlpatterns)

docs/tutorial/4-authentication-and-permissions.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ Make sure to also import the `UserSerializer` class
8585

8686
from snippets.serializers import UserSerializer
8787

88-
Finally we need to add those views into the API, by referencing them from the URL conf. Add the following to the patterns in `urls.py`.
88+
Finally we need to add those views into the API, by referencing them from the URL conf. Add the following to the patterns in `snippets/urls.py`.
8989

90-
url(r'^users/$', views.UserList.as_view()),
91-
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
90+
path('users/', views.UserList.as_view()),
91+
path('users/<int:pk>/', views.UserDetail.as_view()),
9292

9393
## Associating Snippets with Users
9494

@@ -142,10 +142,10 @@ Add the following import at the top of the file:
142142
And, at the end of the file, add a pattern to include the login and logout views for the browsable API.
143143

144144
urlpatterns += [
145-
url(r'^api-auth/', include('rest_framework.urls')),
145+
path('api-auth/', include('rest_framework.urls')),
146146
]
147147

148-
The `r'^api-auth/'` part of pattern can actually be whatever URL you want to use.
148+
The `'api-auth/'` part of pattern can actually be whatever URL you want to use.
149149

150150
Now if you open up the browser again and refresh the page you'll see a 'Login' link in the top right of the page. If you log in as one of the users you created earlier, you'll be able to create code snippets again.
151151

docs/tutorial/5-relationships-and-hyperlinked-apis.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ Instead of using a concrete generic view, we'll use the base class for represent
4444
As usual we need to add the new views that we've created in to our URLconf.
4545
We'll add a url pattern for our new API root in `snippets/urls.py`:
4646

47-
url(r'^$', views.api_root),
47+
path('', views.api_root),
4848

4949
And then add a url pattern for the snippet highlights:
5050

51-
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', views.SnippetHighlight.as_view()),
51+
path('snippets/<int:pk>/highlight/', views.SnippetHighlight.as_view()),
5252

5353
## Hyperlinking our API
5454

@@ -112,20 +112,20 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
112112

113113
# API endpoints
114114
urlpatterns = format_suffix_patterns([
115-
url(r'^$', views.api_root),
116-
url(r'^snippets/$',
115+
path('', views.api_root),
116+
path('snippets/',
117117
views.SnippetList.as_view(),
118118
name='snippet-list'),
119-
url(r'^snippets/(?P<pk>[0-9]+)/$',
119+
path('snippets/<int:pk>/',
120120
views.SnippetDetail.as_view(),
121121
name='snippet-detail'),
122-
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$',
122+
path('snippets/<int:pk>/highlight/',
123123
views.SnippetHighlight.as_view(),
124124
name='snippet-highlight'),
125-
url(r'^users/$',
125+
path('users/',
126126
views.UserList.as_view(),
127127
name='user-list'),
128-
url(r'^users/(?P<pk>[0-9]+)/$',
128+
path('users/<int:pk>/',
129129
views.UserDetail.as_view(),
130130
name='user-detail')
131131
])

docs/tutorial/6-viewsets-and-routers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ Notice how we're creating multiple views from each `ViewSet` class, by binding t
9191
Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.
9292

9393
urlpatterns = format_suffix_patterns([
94-
url(r'^$', api_root),
95-
url(r'^snippets/$', snippet_list, name='snippet-list'),
96-
url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'),
97-
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
98-
url(r'^users/$', user_list, name='user-list'),
99-
url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail')
94+
path('', api_root),
95+
path('snippets/', snippet_list, name='snippet-list'),
96+
path('snippets/<int:pk>/', snippet_detail, name='snippet-detail'),
97+
path('snippets/<int:pk>/highlight/', snippet_highlight, name='snippet-highlight'),
98+
path('users/', user_list, name='user-list'),
99+
path('users/<int:pk>/', user_detail, name='user-detail')
100100
])
101101

102102
## Using Routers

docs/tutorial/7-schemas-and-client-libraries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ from rest_framework.schemas import get_schema_view
4242
schema_view = get_schema_view(title='Pastebin API')
4343

4444
urlpatterns = [
45-
   url(r'^schema/$', schema_view),
45+
   path('schema/', schema_view),
4646
...
4747
]
4848
```

0 commit comments

Comments
 (0)