Skip to content

Commit

Permalink
Replace all url() calls with path() or re_path() (#7512)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
adamchainz and sanjusci authored Sep 8, 2020
1 parent 9990b59 commit 410575d
Show file tree
Hide file tree
Showing 46 changed files with 243 additions and 262 deletions.
4 changes: 2 additions & 2 deletions docs/api-guide/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ When using `TokenAuthentication`, you may want to provide a mechanism for client

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

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

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


Expand Down
2 changes: 1 addition & 1 deletion docs/api-guide/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Another style of filtering might involve restricting the queryset based on some

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions docs/api-guide/format-suffixes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Example:
from blog import views

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

urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
Expand Down
2 changes: 1 addition & 1 deletion docs/api-guide/generic-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ For more complex cases you might also want to override various methods on the vi

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:

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

---

Expand Down
2 changes: 1 addition & 1 deletion docs/api-guide/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ If it is called without a `filename` URL keyword argument, then the client must
# urls.py
urlpatterns = [
# ...
url(r'^upload/(?P<filename>[^/]+)$', FileUploadView.as_view())
re_path(r'^upload/(?P<filename>[^/]+)$', FileUploadView.as_view())
]

---
Expand Down
14 changes: 7 additions & 7 deletions docs/api-guide/routers.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,30 @@ For example, you can append `router.urls` to a list of existing views...
router.register(r'accounts', AccountViewSet)

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

urlpatterns += router.urls

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

urlpatterns = [
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
url(r'^', include(router.urls)),
path('forgot-password', ForgotPasswordFormView.as_view()),
path('', include(router.urls)),
]

You may use `include` with an application namespace:

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

Or both an application and instance namespace:

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

See Django's [URL namespaces docs][url-namespace-docs] and the [`include` API reference][include-api-reference] for more details.
Expand Down
2 changes: 1 addition & 1 deletion docs/api-guide/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ The `get_schema_view()` helper takes the following keyword arguments:
only want the `myproject.api` urls to be exposed in the schema:

schema_url_patterns = [
url(r'^api/', include('myproject.api.urls')),
path('api/', include('myproject.api.urls')),
]

schema_view = get_schema_view(
Expand Down
6 changes: 3 additions & 3 deletions docs/community/3.5-announcement.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ schema_view = get_schema_view(
)

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

urlpatterns = [
url('^$', schema_view),
url(r'^', include(router.urls)),
path('', schema_view),
path('', include(router.urls)),
]

### Schema path representations
Expand Down
2 changes: 1 addition & 1 deletion docs/community/3.6-announcement.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ To install the API documentation, you'll need to include it in your projects URL

urlpatterns = [
...
url(r'^docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION))
path('docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION))
]

Once installed you should see something a little like this:
Expand Down
3 changes: 2 additions & 1 deletion docs/community/3.9-announcement.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Here's an example of adding an OpenAPI schema to the URL conf:
```python
from rest_framework.schemas import get_schema_view
from rest_framework.renderers import JSONOpenAPIRenderer
from django.urls import path

schema_view = get_schema_view(
title='Server Monitoring API',
Expand All @@ -70,7 +71,7 @@ schema_view = get_schema_view(
)

urlpatterns = [
url('^schema.json$', schema_view),
path('schema.json', schema_view),
...
]
```
Expand Down
4 changes: 2 additions & 2 deletions docs/coreapi/from-documenting-your-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To install the API documentation, you'll need to include it in your project's UR

urlpatterns = [
...
url(r'^docs/', include_docs_urls(title='My API title'))
path('docs/', include_docs_urls(title='My API title'))
]

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


Expand Down
9 changes: 5 additions & 4 deletions docs/coreapi/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ To add a dynamically generated schema view to your API, use `get_schema_view`.

```python
from rest_framework.schemas import get_schema_view
from django.urls import path

schema_view = get_schema_view(title="Example API")

urlpatterns = [
url('^schema$', schema_view),
path('schema', schema_view),
...
]
```
Expand Down Expand Up @@ -292,7 +293,7 @@ The simplest way to include a schema in your project is to use the
schema_view = get_schema_view(title="Server Monitoring API")

urlpatterns = [
url('^$', schema_view),
path('', schema_view),
...
]

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

schema_url_patterns = [
url(r'^api/', include('myproject.api.urls')),
path('api/', include('myproject.api.urls')),
]

schema_view = get_schema_view(
Expand Down Expand Up @@ -411,7 +412,7 @@ return the schema.
**urls.py:**

urlpatterns = [
url('/', schema_view),
path('', schema_view),
...
]

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ If you're intending to use the browsable API you'll probably also want to add RE

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

Note that the URL path can be whatever you want.
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/api-clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ First, install the API documentation views. These will include the schema resour

urlpatterns = [
...
url(r'^docs/', include_docs_urls(title='My API service'), name='api-docs'),
path('docs/', include_docs_urls(title='My API service'), name='api-docs'),
]

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.
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/4-authentication-and-permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ We can add a login view for use with the browsable API, by editing the URLconf i

Add the following import at the top of the file:

from django.conf.urls import include
from django.urls import path, include

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

Expand Down
2 changes: 1 addition & 1 deletion rest_framework/templates/rest_framework/docs/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h3>403 Forbidden.</h3>
when including the docs urls:</p>

<pre>
url(r'^docs/', include_docs_urls(title='Your API',
path('docs/', include_docs_urls(title='Your API',
authentication_classes=[],
permission_classes=[])),
</pre>
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
urlpatterns = [
...
url(r'^auth/', include('rest_framework.urls'))
path('auth/', include('rest_framework.urls'))
]
You should make sure your authentication settings include `SessionAuthentication`.
Expand Down
12 changes: 6 additions & 6 deletions rest_framework/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class URLPathVersioning(BaseVersioning):
An example URL conf for two views that accept two different versions.
urlpatterns = [
url(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
url(r'^(?P<version>[v1|v2]+)/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
re_path(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
re_path(r'^(?P<version>[v1|v2]+)/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
]
GET /1.0/something/ HTTP/1.1
Expand Down Expand Up @@ -99,14 +99,14 @@ class NamespaceVersioning(BaseVersioning):
# users/urls.py
urlpatterns = [
url(r'^/users/$', users_list, name='users-list'),
url(r'^/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
path('/users/', users_list, name='users-list'),
path('/users/<int:pk>/', users_detail, name='users-detail')
]
# urls.py
urlpatterns = [
url(r'^v1/', include('users.urls', namespace='v1')),
url(r'^v2/', include('users.urls', namespace='v2'))
path('v1/', include('users.urls', namespace='v1')),
path('v2/', include('users.urls', namespace='v2'))
]
GET /1.0/something/ HTTP/1.1
Expand Down
30 changes: 15 additions & 15 deletions tests/authentication/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import pytest
from django.conf import settings
from django.conf.urls import include, url
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.test import TestCase, override_settings
from django.urls import include, path

from rest_framework import (
HTTP_HEADER_ENCODING, exceptions, permissions, renderers, status
Expand Down Expand Up @@ -47,34 +47,34 @@ def put(self, request):


urlpatterns = [
url(
r'^session/$',
path(
'session/',
MockView.as_view(authentication_classes=[SessionAuthentication])
),
url(
r'^basic/$',
path(
'basic/',
MockView.as_view(authentication_classes=[BasicAuthentication])
),
url(
r'^remote-user/$',
path(
'remote-user/',
MockView.as_view(authentication_classes=[RemoteUserAuthentication])
),
url(
r'^token/$',
path(
'token/',
MockView.as_view(authentication_classes=[TokenAuthentication])
),
url(
r'^customtoken/$',
path(
'customtoken/',
MockView.as_view(authentication_classes=[CustomTokenAuthentication])
),
url(
r'^customkeywordtoken/$',
path(
'customkeywordtoken/',
MockView.as_view(
authentication_classes=[CustomKeywordTokenAuthentication]
)
),
url(r'^auth-token/$', obtain_auth_token),
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
path('auth-token/', obtain_auth_token),
path('auth/', include('rest_framework.urls', namespace='rest_framework')),
]


Expand Down
6 changes: 3 additions & 3 deletions tests/browsable_api/auth_urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.conf.urls import include, url
from django.urls import include, path

from .views import MockView

urlpatterns = [
url(r'^$', MockView.as_view()),
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
path('', MockView.as_view()),
path('auth/', include('rest_framework.urls', namespace='rest_framework')),
]
4 changes: 2 additions & 2 deletions tests/browsable_api/no_auth_urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import url
from django.urls import path

from .views import MockView

urlpatterns = [
url(r'^$', MockView.as_view()),
path('', MockView.as_view()),
]
4 changes: 2 additions & 2 deletions tests/browsable_api/test_browsable_nested_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf.urls import url
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import path

from rest_framework import serializers
from rest_framework.generics import ListCreateAPIView
Expand All @@ -23,7 +23,7 @@ class NestedSerializersView(ListCreateAPIView):


urlpatterns = [
url(r'^api/$', NestedSerializersView.as_view(), name='api'),
path('api/', NestedSerializersView.as_view(), name='api'),
]


Expand Down
Loading

0 comments on commit 410575d

Please sign in to comment.