Skip to content

Commit 410575d

Browse files
adamchainzsanjusci
andauthored
Replace all url() calls with path() or re_path() (encode#7512)
* url() is deprecated in Django 3.1 * update given feedbacks on url() is deprecated in Django 3.1 * Fix test_urlpatterns.py to continue testing mixed re_path() and path() * Fix one missed reference Co-authored-by: sanjusci <sanju.sci9@gmail.com>
1 parent 9990b59 commit 410575d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+243
-262
lines changed

docs/api-guide/authentication.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ When using `TokenAuthentication`, you may want to provide a mechanism for client
199199

200200
from rest_framework.authtoken import views
201201
urlpatterns += [
202-
url(r'^api-token-auth/', views.obtain_auth_token)
202+
path('api-token-auth/', views.obtain_auth_token)
203203
]
204204

205205
Note that the URL part of the pattern can be whatever you want to use.
@@ -238,7 +238,7 @@ For example, you may return additional user information beyond the `token` value
238238
And in your `urls.py`:
239239

240240
urlpatterns += [
241-
url(r'^api-token-auth/', CustomAuthToken.as_view())
241+
path('api-token-auth/', CustomAuthToken.as_view())
242242
]
243243

244244

docs/api-guide/filtering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Another style of filtering might involve restricting the queryset based on some
4545

4646
For example if your URL config contained an entry like this:
4747

48-
url('^purchases/(?P<username>.+)/$', PurchaseList.as_view()),
48+
re_path('^purchases/(?P<username>.+)/$', PurchaseList.as_view()),
4949

5050
You could then write a view that returned a purchase queryset filtered by the username portion of the URL:
5151

docs/api-guide/format-suffixes.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Example:
3232
from blog import views
3333

3434
urlpatterns = [
35-
url(r'^/$', views.apt_root),
36-
url(r'^comments/$', views.comment_list),
37-
url(r'^comments/(?P<pk>[0-9]+)/$', views.comment_detail)
35+
path('', views.apt_root),
36+
path('comments/', views.comment_list),
37+
path('comments/<int:pk>/', views.comment_detail)
3838
]
3939

4040
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])

docs/api-guide/generic-views.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ For more complex cases you might also want to override various methods on the vi
4545

4646
For very simple cases you might want to pass through any class attributes using the `.as_view()` method. For example, your URLconf might include something like the following entry:
4747

48-
url(r'^/users/', ListCreateAPIView.as_view(queryset=User.objects.all(), serializer_class=UserSerializer), name='user-list')
48+
path('users/', ListCreateAPIView.as_view(queryset=User.objects.all(), serializer_class=UserSerializer), name='user-list')
4949

5050
---
5151

docs/api-guide/parsers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ If it is called without a `filename` URL keyword argument, then the client must
125125
# urls.py
126126
urlpatterns = [
127127
# ...
128-
url(r'^upload/(?P<filename>[^/]+)$', FileUploadView.as_view())
128+
re_path(r'^upload/(?P<filename>[^/]+)$', FileUploadView.as_view())
129129
]
130130

131131
---

docs/api-guide/routers.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,30 @@ For example, you can append `router.urls` to a list of existing views...
6363
router.register(r'accounts', AccountViewSet)
6464

6565
urlpatterns = [
66-
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
66+
path('forgot-password/', ForgotPasswordFormView.as_view()),
6767
]
6868

6969
urlpatterns += router.urls
7070

7171
Alternatively you can use Django's `include` function, like so...
7272

7373
urlpatterns = [
74-
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
75-
url(r'^', include(router.urls)),
74+
path('forgot-password', ForgotPasswordFormView.as_view()),
75+
path('', include(router.urls)),
7676
]
7777

7878
You may use `include` with an application namespace:
7979

8080
urlpatterns = [
81-
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
82-
url(r'^api/', include((router.urls, 'app_name'))),
81+
path('forgot-password/', ForgotPasswordFormView.as_view()),
82+
path('api/', include((router.urls, 'app_name'))),
8383
]
8484

8585
Or both an application and instance namespace:
8686

8787
urlpatterns = [
88-
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
89-
url(r'^api/', include((router.urls, 'app_name'), namespace='instance_name')),
88+
path('forgot-password/', ForgotPasswordFormView.as_view()),
89+
path('api/', include((router.urls, 'app_name'), namespace='instance_name')),
9090
]
9191

9292
See Django's [URL namespaces docs][url-namespace-docs] and the [`include` API reference][include-api-reference] for more details.

docs/api-guide/schemas.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ The `get_schema_view()` helper takes the following keyword arguments:
114114
only want the `myproject.api` urls to be exposed in the schema:
115115

116116
schema_url_patterns = [
117-
url(r'^api/', include('myproject.api.urls')),
117+
path('api/', include('myproject.api.urls')),
118118
]
119119

120120
schema_view = get_schema_view(

docs/community/3.5-announcement.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ schema_view = get_schema_view(
6969
)
7070

7171
urlpatterns = [
72-
url(r'^swagger/$', schema_view),
72+
path('swagger/', schema_view),
7373
...
7474
]
7575
```
@@ -198,8 +198,8 @@ Make sure to include the view before your router urls. For example:
198198
schema_view = get_schema_view(title='Example API')
199199

200200
urlpatterns = [
201-
url('^$', schema_view),
202-
url(r'^', include(router.urls)),
201+
path('', schema_view),
202+
path('', include(router.urls)),
203203
]
204204

205205
### Schema path representations

docs/community/3.6-announcement.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ To install the API documentation, you'll need to include it in your projects URL
7373

7474
urlpatterns = [
7575
...
76-
url(r'^docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION))
76+
path('docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION))
7777
]
7878

7979
Once installed you should see something a little like this:

docs/community/3.9-announcement.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Here's an example of adding an OpenAPI schema to the URL conf:
6262
```python
6363
from rest_framework.schemas import get_schema_view
6464
from rest_framework.renderers import JSONOpenAPIRenderer
65+
from django.urls import path
6566

6667
schema_view = get_schema_view(
6768
title='Server Monitoring API',
@@ -70,7 +71,7 @@ schema_view = get_schema_view(
7071
)
7172

7273
urlpatterns = [
73-
url('^schema.json$', schema_view),
74+
path('schema.json', schema_view),
7475
...
7576
]
7677
```

docs/coreapi/from-documenting-your-api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To install the API documentation, you'll need to include it in your project's UR
1919

2020
urlpatterns = [
2121
...
22-
url(r'^docs/', include_docs_urls(title='My API title'))
22+
path('docs/', include_docs_urls(title='My API title'))
2323
]
2424

2525
This will include two different views:
@@ -41,7 +41,7 @@ You may ensure views are given a `request` instance by calling `include_docs_url
4141
urlpatterns = [
4242
...
4343
# Generate schema with valid `request` instance:
44-
url(r'^docs/', include_docs_urls(title='My API title', public=False))
44+
path('docs/', include_docs_urls(title='My API title', public=False))
4545
]
4646

4747

docs/coreapi/schemas.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ To add a dynamically generated schema view to your API, use `get_schema_view`.
4343

4444
```python
4545
from rest_framework.schemas import get_schema_view
46+
from django.urls import path
4647

4748
schema_view = get_schema_view(title="Example API")
4849

4950
urlpatterns = [
50-
url('^schema$', schema_view),
51+
path('schema', schema_view),
5152
...
5253
]
5354
```
@@ -292,7 +293,7 @@ The simplest way to include a schema in your project is to use the
292293
schema_view = get_schema_view(title="Server Monitoring API")
293294

294295
urlpatterns = [
295-
url('^$', schema_view),
296+
path('', schema_view),
296297
...
297298
]
298299

@@ -358,7 +359,7 @@ List of url patterns to limit the schema introspection to. If you only want the
358359
to be exposed in the schema:
359360

360361
schema_url_patterns = [
361-
url(r'^api/', include('myproject.api.urls')),
362+
path('api/', include('myproject.api.urls')),
362363
]
363364

364365
schema_view = get_schema_view(
@@ -411,7 +412,7 @@ return the schema.
411412
**urls.py:**
412413

413414
urlpatterns = [
414-
url('/', schema_view),
415+
path('', schema_view),
415416
...
416417
]
417418

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ If you're intending to use the browsable API you'll probably also want to add RE
120120

121121
urlpatterns = [
122122
...
123-
url(r'^api-auth/', include('rest_framework.urls'))
123+
path('api-auth/', include('rest_framework.urls'))
124124
]
125125

126126
Note that the URL path can be whatever you want.

docs/topics/api-clients.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ First, install the API documentation views. These will include the schema resour
384384

385385
urlpatterns = [
386386
...
387-
url(r'^docs/', include_docs_urls(title='My API service'), name='api-docs'),
387+
path('docs/', include_docs_urls(title='My API service'), name='api-docs'),
388388
]
389389

390390
Once the API documentation URLs are installed, you'll be able to include both the required JavaScript resources. Note that the ordering of these two lines is important, as the schema loading requires CoreAPI to already be installed.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ We can add a login view for use with the browsable API, by editing the URLconf i
137137

138138
Add the following import at the top of the file:
139139

140-
from django.conf.urls import include
140+
from django.urls import path, include
141141

142142
And, at the end of the file, add a pattern to include the login and logout views for the browsable API.
143143

rest_framework/templates/rest_framework/docs/error.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ <h3>403 Forbidden.</h3>
4848
when including the docs urls:</p>
4949

5050
<pre>
51-
url(r'^docs/', include_docs_urls(title='Your API',
51+
path('docs/', include_docs_urls(title='Your API',
5252
authentication_classes=[],
5353
permission_classes=[])),
5454
</pre>

rest_framework/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
urlpatterns = [
88
...
9-
url(r'^auth/', include('rest_framework.urls'))
9+
path('auth/', include('rest_framework.urls'))
1010
]
1111
1212
You should make sure your authentication settings include `SessionAuthentication`.

rest_framework/versioning.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class URLPathVersioning(BaseVersioning):
6060
An example URL conf for two views that accept two different versions.
6161
6262
urlpatterns = [
63-
url(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
64-
url(r'^(?P<version>[v1|v2]+)/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
63+
re_path(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
64+
re_path(r'^(?P<version>[v1|v2]+)/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
6565
]
6666
6767
GET /1.0/something/ HTTP/1.1
@@ -99,14 +99,14 @@ class NamespaceVersioning(BaseVersioning):
9999
100100
# users/urls.py
101101
urlpatterns = [
102-
url(r'^/users/$', users_list, name='users-list'),
103-
url(r'^/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
102+
path('/users/', users_list, name='users-list'),
103+
path('/users/<int:pk>/', users_detail, name='users-detail')
104104
]
105105
106106
# urls.py
107107
urlpatterns = [
108-
url(r'^v1/', include('users.urls', namespace='v1')),
109-
url(r'^v2/', include('users.urls', namespace='v2'))
108+
path('v1/', include('users.urls', namespace='v1')),
109+
path('v2/', include('users.urls', namespace='v2'))
110110
]
111111
112112
GET /1.0/something/ HTTP/1.1

tests/authentication/test_authentication.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import pytest
44
from django.conf import settings
5-
from django.conf.urls import include, url
65
from django.contrib.auth.models import User
76
from django.http import HttpResponse
87
from django.test import TestCase, override_settings
8+
from django.urls import include, path
99

1010
from rest_framework import (
1111
HTTP_HEADER_ENCODING, exceptions, permissions, renderers, status
@@ -47,34 +47,34 @@ def put(self, request):
4747

4848

4949
urlpatterns = [
50-
url(
51-
r'^session/$',
50+
path(
51+
'session/',
5252
MockView.as_view(authentication_classes=[SessionAuthentication])
5353
),
54-
url(
55-
r'^basic/$',
54+
path(
55+
'basic/',
5656
MockView.as_view(authentication_classes=[BasicAuthentication])
5757
),
58-
url(
59-
r'^remote-user/$',
58+
path(
59+
'remote-user/',
6060
MockView.as_view(authentication_classes=[RemoteUserAuthentication])
6161
),
62-
url(
63-
r'^token/$',
62+
path(
63+
'token/',
6464
MockView.as_view(authentication_classes=[TokenAuthentication])
6565
),
66-
url(
67-
r'^customtoken/$',
66+
path(
67+
'customtoken/',
6868
MockView.as_view(authentication_classes=[CustomTokenAuthentication])
6969
),
70-
url(
71-
r'^customkeywordtoken/$',
70+
path(
71+
'customkeywordtoken/',
7272
MockView.as_view(
7373
authentication_classes=[CustomKeywordTokenAuthentication]
7474
)
7575
),
76-
url(r'^auth-token/$', obtain_auth_token),
77-
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
76+
path('auth-token/', obtain_auth_token),
77+
path('auth/', include('rest_framework.urls', namespace='rest_framework')),
7878
]
7979

8080

tests/browsable_api/auth_urls.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from django.conf.urls import include, url
1+
from django.urls import include, path
22

33
from .views import MockView
44

55
urlpatterns = [
6-
url(r'^$', MockView.as_view()),
7-
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
6+
path('', MockView.as_view()),
7+
path('auth/', include('rest_framework.urls', namespace='rest_framework')),
88
]

tests/browsable_api/no_auth_urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from django.conf.urls import url
1+
from django.urls import path
22

33
from .views import MockView
44

55
urlpatterns = [
6-
url(r'^$', MockView.as_view()),
6+
path('', MockView.as_view()),
77
]

tests/browsable_api/test_browsable_nested_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from django.conf.urls import url
21
from django.test import TestCase
32
from django.test.utils import override_settings
3+
from django.urls import path
44

55
from rest_framework import serializers
66
from rest_framework.generics import ListCreateAPIView
@@ -23,7 +23,7 @@ class NestedSerializersView(ListCreateAPIView):
2323

2424

2525
urlpatterns = [
26-
url(r'^api/$', NestedSerializersView.as_view(), name='api'),
26+
path('api/', NestedSerializersView.as_view(), name='api'),
2727
]
2828

2929

0 commit comments

Comments
 (0)