mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-30 18:09:59 +03:00
update some examples to path() instead of re_path
This commit is contained in:
parent
8a259a84fd
commit
9751348d4d
|
@ -113,8 +113,8 @@ router.register(r'users', UserViewSet)
|
|||
# Wire up our API using automatic URL routing.
|
||||
# Additionally, we include login URLs for the browsable API.
|
||||
urlpatterns = [
|
||||
re_path(r'^', include(router.urls)),
|
||||
re_path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||
path('', include(router.urls)),
|
||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||
]
|
||||
```
|
||||
|
||||
|
|
|
@ -196,7 +196,7 @@ When using `TokenAuthentication`, you may want to provide a mechanism for client
|
|||
|
||||
from rest_framework.authtoken import views
|
||||
urlpatterns += [
|
||||
re_path(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.
|
||||
|
@ -235,7 +235,7 @@ For example, you may return additional user information beyond the `token` value
|
|||
And in your `urls.py`:
|
||||
|
||||
urlpatterns += [
|
||||
re_path(r'^api-token-auth/', CustomAuthToken.as_view())
|
||||
path('api-token-auth/', CustomAuthToken.as_view())
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
source: mixins.py
|
||||
generics.py
|
||||
generics.py
|
||||
|
||||
# Generic views
|
||||
|
||||
|
@ -42,7 +42,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:
|
||||
|
||||
re_path(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')
|
||||
|
||||
---
|
||||
|
||||
|
@ -60,20 +60,20 @@ Each of the concrete generic views provided is built by combining `GenericAPIVie
|
|||
|
||||
The following attributes control the basic view behavior.
|
||||
|
||||
* `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method. If you are overriding a view method, it is important that you call `get_queryset()` instead of accessing this property directly, as `queryset` will get evaluated once, and those results will be cached for all subsequent requests.
|
||||
* `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method.
|
||||
* `lookup_field` - The model field that should be used to for performing object lookup of individual model instances. Defaults to `'pk'`. Note that when using hyperlinked APIs you'll need to ensure that *both* the API views *and* the serializer classes set the lookup fields if you need to use a custom value.
|
||||
* `lookup_url_kwarg` - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as `lookup_field`.
|
||||
- `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method. If you are overriding a view method, it is important that you call `get_queryset()` instead of accessing this property directly, as `queryset` will get evaluated once, and those results will be cached for all subsequent requests.
|
||||
- `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method.
|
||||
- `lookup_field` - The model field that should be used to for performing object lookup of individual model instances. Defaults to `'pk'`. Note that when using hyperlinked APIs you'll need to ensure that _both_ the API views _and_ the serializer classes set the lookup fields if you need to use a custom value.
|
||||
- `lookup_url_kwarg` - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as `lookup_field`.
|
||||
|
||||
**Pagination**:
|
||||
|
||||
The following attributes are used to control pagination when used with list views.
|
||||
|
||||
* `pagination_class` - The pagination class that should be used when paginating list results. Defaults to the same value as the `DEFAULT_PAGINATION_CLASS` setting, which is `'rest_framework.pagination.PageNumberPagination'`. Setting `pagination_class=None` will disable pagination on this view.
|
||||
- `pagination_class` - The pagination class that should be used when paginating list results. Defaults to the same value as the `DEFAULT_PAGINATION_CLASS` setting, which is `'rest_framework.pagination.PageNumberPagination'`. Setting `pagination_class=None` will disable pagination on this view.
|
||||
|
||||
**Filtering**:
|
||||
|
||||
* `filter_backends` - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the `DEFAULT_FILTER_BACKENDS` setting.
|
||||
- `filter_backends` - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the `DEFAULT_FILTER_BACKENDS` setting.
|
||||
|
||||
### Methods
|
||||
|
||||
|
@ -149,9 +149,9 @@ For example:
|
|||
|
||||
The following methods are provided by the mixin classes, and provide easy overriding of the object save or deletion behavior.
|
||||
|
||||
* `perform_create(self, serializer)` - Called by `CreateModelMixin` when saving a new object instance.
|
||||
* `perform_update(self, serializer)` - Called by `UpdateModelMixin` when saving an existing object instance.
|
||||
* `perform_destroy(self, instance)` - Called by `DestroyModelMixin` when deleting an object instance.
|
||||
- `perform_create(self, serializer)` - Called by `CreateModelMixin` when saving a new object instance.
|
||||
- `perform_update(self, serializer)` - Called by `UpdateModelMixin` when saving an existing object instance.
|
||||
- `perform_destroy(self, instance)` - Called by `DestroyModelMixin` when deleting an object instance.
|
||||
|
||||
These hooks are particularly useful for setting attributes that are implicit in the request, but are not part of the request data. For instance, you might set an attribute on the object based on the request user, or based on a URL keyword argument.
|
||||
|
||||
|
@ -178,11 +178,11 @@ You can also use these hooks to provide additional validation, by raising a `Val
|
|||
|
||||
You won't typically need to override the following methods, although you might need to call into them if you're writing custom views using `GenericAPIView`.
|
||||
|
||||
* `get_serializer_context(self)` - Returns a dictionary containing any extra context that should be supplied to the serializer. Defaults to including `'request'`, `'view'` and `'format'` keys.
|
||||
* `get_serializer(self, instance=None, data=None, many=False, partial=False)` - Returns a serializer instance.
|
||||
* `get_paginated_response(self, data)` - Returns a paginated style `Response` object.
|
||||
* `paginate_queryset(self, queryset)` - Paginate a queryset if required, either returning a page object, or `None` if pagination is not configured for this view.
|
||||
* `filter_queryset(self, queryset)` - Given a queryset, filter it with whichever filter backends are in use, returning a new queryset.
|
||||
- `get_serializer_context(self)` - Returns a dictionary containing any extra context that should be supplied to the serializer. Defaults to including `'request'`, `'view'` and `'format'` keys.
|
||||
- `get_serializer(self, instance=None, data=None, many=False, partial=False)` - Returns a serializer instance.
|
||||
- `get_paginated_response(self, data)` - Returns a paginated style `Response` object.
|
||||
- `paginate_queryset(self, queryset)` - Paginate a queryset if required, either returning a page object, or `None` if pagination is not configured for this view.
|
||||
- `filter_queryset(self, queryset)` - Given a queryset, filter it with whichever filter backends are in use, returning a new queryset.
|
||||
|
||||
---
|
||||
|
||||
|
@ -383,13 +383,12 @@ The [django-rest-framework-bulk package][django-rest-framework-bulk] implements
|
|||
|
||||
[Django Rest Multiple Models][django-rest-multiple-models] provides a generic view (and mixin) for sending multiple serialized models and/or querysets via a single API request.
|
||||
|
||||
|
||||
[cite]: https://docs.djangoproject.com/en/stable/ref/class-based-views/#base-vs-generic-views
|
||||
[GenericAPIView]: #genericapiview
|
||||
[ListModelMixin]: #listmodelmixin
|
||||
[CreateModelMixin]: #createmodelmixin
|
||||
[RetrieveModelMixin]: #retrievemodelmixin
|
||||
[UpdateModelMixin]: #updatemodelmixin
|
||||
[DestroyModelMixin]: #destroymodelmixin
|
||||
[genericapiview]: #genericapiview
|
||||
[listmodelmixin]: #listmodelmixin
|
||||
[createmodelmixin]: #createmodelmixin
|
||||
[retrievemodelmixin]: #retrievemodelmixin
|
||||
[updatemodelmixin]: #updatemodelmixin
|
||||
[destroymodelmixin]: #destroymodelmixin
|
||||
[django-rest-framework-bulk]: https://github.com/miki725/django-rest-framework-bulk
|
||||
[django-rest-multiple-models]: https://github.com/MattBroach/DjangoRestMultipleModels
|
||||
|
|
|
@ -69,21 +69,21 @@ Alternatively you can use Django's `include` function, like so...
|
|||
|
||||
urlpatterns = [
|
||||
re_path(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
|
||||
re_path(r'^', include(router.urls)),
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
|
||||
You may use `include` with an application namespace:
|
||||
|
||||
urlpatterns = [
|
||||
re_path(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
|
||||
re_path(r'^api/', include((router.urls, 'app_name'))),
|
||||
path('api/', include((router.urls, 'app_name'))),
|
||||
]
|
||||
|
||||
Or both an application and instance namespace:
|
||||
|
||||
urlpatterns = [
|
||||
re_path(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
|
||||
re_path(r'^api/', include((router.urls, 'app_name'), namespace='instance_name')),
|
||||
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.
|
||||
|
|
|
@ -358,7 +358,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 = [
|
||||
re_path(r'^api/', include('myproject.api.urls')),
|
||||
path('api/', include('myproject.api.urls')),
|
||||
]
|
||||
|
||||
schema_view = get_schema_view(
|
||||
|
|
|
@ -161,8 +161,8 @@ In the following example we're giving a set of views two different possible URL
|
|||
|
||||
# urls.py
|
||||
urlpatterns = [
|
||||
re_path(r'^v1/bookings/', include('bookings.urls', namespace='v1')),
|
||||
re_path(r'^v2/bookings/', include('bookings.urls', namespace='v2'))
|
||||
path('v1/bookings/', include('bookings.urls', namespace='v1')),
|
||||
path('v2/bookings/', include('bookings.urls', namespace='v2'))
|
||||
]
|
||||
|
||||
Both `URLPathVersioning` and `NamespaceVersioning` are reasonable if you just need a simple versioning scheme. The `URLPathVersioning` approach might be better suitable for small ad-hoc projects, and the `NamespaceVersioning` is probably easier to manage for larger projects.
|
||||
|
|
|
@ -199,7 +199,7 @@ Make sure to include the view before your router urls. For example:
|
|||
|
||||
urlpatterns = [
|
||||
url('^$', schema_view),
|
||||
re_path(r'^', include(router.urls)),
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
|
||||
### Schema path representations
|
||||
|
|
|
@ -73,7 +73,7 @@ To install the API documentation, you'll need to include it in your projects URL
|
|||
|
||||
urlpatterns = [
|
||||
...
|
||||
re_path(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:
|
||||
|
|
|
@ -121,7 +121,7 @@ If you're intending to use the browsable API you'll probably also want to add RE
|
|||
|
||||
urlpatterns = [
|
||||
...
|
||||
re_path(r'^api-auth/', include('rest_framework.urls'))
|
||||
path('api-auth/', include('rest_framework.urls'))
|
||||
]
|
||||
|
||||
Note that the URL path can be whatever you want.
|
||||
|
@ -169,8 +169,8 @@ Here's our project's root `urls.py` module:
|
|||
# Wire up our API using automatic URL routing.
|
||||
# Additionally, we include login URLs for the browsable API.
|
||||
urlpatterns = [
|
||||
re_path(r'^', include(router.urls)),
|
||||
re_path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||
path('', include(router.urls)),
|
||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||
]
|
||||
|
||||
You can now open the API in your browser at [http://127.0.0.1:8000/](http://127.0.0.1:8000/), and view your new 'users' API. If you use the login control in the top right corner you'll also be able to add, create and delete users from the system.
|
||||
|
|
|
@ -384,7 +384,7 @@ First, install the API documentation views. These will include the schema resour
|
|||
|
||||
urlpatterns = [
|
||||
...
|
||||
re_path(r'^docs/', include_docs_urls(title='My API service'))
|
||||
path('docs/', include_docs_urls(title='My API service'))
|
||||
]
|
||||
|
||||
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.
|
||||
|
|
|
@ -26,7 +26,7 @@ To install the API documentation, you'll need to include it in your project's UR
|
|||
|
||||
urlpatterns = [
|
||||
...
|
||||
re_path(r'^docs/', include_docs_urls(title='My API title'))
|
||||
path('docs/', include_docs_urls(title='My API title'))
|
||||
]
|
||||
|
||||
This will include two different views:
|
||||
|
@ -48,7 +48,7 @@ You may ensure views are given a `request` instance by calling `include_docs_url
|
|||
urlpatterns = [
|
||||
...
|
||||
# Generate schema with valid `request` instance:
|
||||
re_path(r'^docs/', include_docs_urls(title='My API title', public=False))
|
||||
path('docs/', include_docs_urls(title='My API title', public=False))
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ being applied unexpectedly?</p>
|
|||
when including the docs urls:</p>
|
||||
|
||||
<pre>
|
||||
re_path(r'^docs/', include_docs_urls(title='Your API',
|
||||
path('docs/', include_docs_urls(title='Your API',
|
||||
authentication_classes=[],
|
||||
permission_classes=[])),
|
||||
</pre>
|
||||
|
|
Loading…
Reference in New Issue
Block a user