2016-06-24 13:05:19 +03:00
< style >
.promo li a {
float: left;
width: 130px;
height: 20px;
text-align: center;
margin: 10px 30px;
padding: 150px 0 0 0;
background-position: 0 50%;
background-size: 130px auto;
background-repeat: no-repeat;
font-size: 120%;
color: black;
}
.promo li {
list-style: none;
}
< / style >
2014-01-03 17:06:41 +04:00
< p class = "badges" height = 20px >
2018-01-08 18:22:32 +03:00
< iframe src = "https://ghbtns.com/github-btn.html?user=encode&repo=django-rest-framework&type=watch&count=true" class = "github-star-button" allowtransparency = "true" frameborder = "0" scrolling = "0" width = "110px" height = "20px" > < / iframe >
2013-01-22 21:35:46 +04:00
2021-04-12 15:14:26 +03:00
< a href = "https://github.com/encode/django-rest-framework/actions/workflows/main.yml" >
< img src = "https://github.com/encode/django-rest-framework/actions/workflows/main.yml/badge.svg" class = "status-badge" >
2015-06-23 13:19:00 +03:00
< / a >
2018-04-19 09:36:18 +03:00
< a href = "https://pypi.org/project/djangorestframework/" >
2015-06-23 13:19:00 +03:00
< img src = "https://img.shields.io/pypi/v/djangorestframework.svg" class = "status-badge" >
< / a >
2013-01-22 21:35:46 +04:00
< / p >
2012-09-09 19:56:04 +04:00
2014-01-03 17:06:41 +04:00
---
< p >
2014-01-14 18:47:34 +04:00
< h1 style = "position: absolute ;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
border: 0;">Django REST Framework< / h1 >
2014-01-14 18:40:36 +04:00
< img alt = "Django REST Framework" title = "Logo by Jake 'Sid' Smith" src = "img/logo.png" width = "600px" style = "display: block; margin: 0 auto 0 auto" >
2014-01-03 17:06:41 +04:00
< / p >
2012-09-01 23:26:27 +04:00
2015-10-22 16:05:52 +03:00
Django REST framework is a powerful and flexible toolkit for building Web APIs.
2012-09-01 23:26:27 +04:00
2013-04-30 12:38:04 +04:00
Some reasons you might want to use REST framework:
2012-09-01 23:26:27 +04:00
2023-11-30 14:07:36 +03:00
* The Web browsable API is a huge usability win for your developers.
2015-01-05 15:26:15 +03:00
* [Authentication policies][authentication] including packages for [OAuth1a][oauth1-section] and [OAuth2][oauth2-section].
2013-06-27 16:41:42 +04:00
* [Serialization][serializers] that supports both [ORM][modelserializer-section] and [non-ORM][serializer-section] data sources.
* Customizable all the way down - just use [regular function-based views][functionview-section] if you don't need the [more][generic-views] [powerful][viewsets] [features][routers].
2019-12-12 17:31:40 +03:00
* Extensive documentation, and [great community support][group].
2016-06-24 13:05:19 +03:00
* Used and trusted by internationally recognised companies including [Mozilla][mozilla], [Red Hat][redhat], [Heroku][heroku], and [Eventbrite][eventbrite].
2012-10-30 16:23:17 +04:00
2014-01-03 17:06:41 +04:00
---
2012-10-30 16:23:17 +04:00
2016-06-24 13:05:19 +03:00
## Funding
2016-05-28 12:55:38 +03:00
2016-06-24 13:05:19 +03:00
REST framework is a *collaboratively funded project* . If you use
REST framework commercially we strongly encourage you to invest in its
continued development by ** [signing up for a paid plan][funding]**.
2017-03-10 08:30:31 +03:00
*Every single sign-up helps us make REST framework long-term financially sustainable.*
2016-06-24 13:05:19 +03:00
< ul class = "premium-promo promo" >
< li > < a href = "https://getsentry.com/welcome/" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/sentry130.png)" > Sentry< / a > < / li >
2022-01-11 16:01:25 +03:00
< li > < a href = "https://getstream.io/?utm_source=DjangoRESTFramework&utm_medium=Webpage_Logo_Ad&utm_content=Developer&utm_campaign=DjangoRESTFramework_Jan2022_HomePage" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/stream-130.png)" > Stream< / a > < / li >
2022-06-06 13:00:01 +03:00
< li > < a href = "https://www.spacinov.com/" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/spacinov.png)" > Spacinov< / a > < / li >
2019-10-07 16:21:58 +03:00
< li > < a href = "https://retool.com/?utm_source=djangorest&utm_medium=sponsorship" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/retool-sidebar.png)" > Retool< / a > < / li >
2020-08-05 16:13:01 +03:00
< li > < a href = "https://bit.io/jobs?utm_source=DRF&utm_medium=sponsor&utm_campaign=DRF_sponsorship" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/bitio_logo_gold_background.png)" > bit.io< / a > < / li >
2021-12-08 18:11:55 +03:00
< li > < a href = "https://posthog.com?utm_source=DRF&utm_medium=sponsor&utm_campaign=DRF_sponsorship" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/135996800-d49fe024-32d9-441a-98d9-4c7596287a67.png)" > PostHog< / a > < / li >
< li > < a href = "https://cryptapi.io" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/cryptapi.png)" > CryptAPI< / a > < / li >
2022-05-26 13:19:09 +03:00
< li > < a href = "https://www.fezto.xyz/?utm_source=DjangoRESTFramework" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/fezto.png)" > FEZTO< / a > < / li >
2023-11-29 17:13:36 +03:00
< li > < a href = "https://www.svix.com/?utm_source=django-REST&utm_medium=sponsorship" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/svix.png)" > Svix< / a > < / li >
2024-07-05 18:56:58 +03:00
< li > < a href = "https://zuplo.link/django-web" style = "background-image: url(https://fund-rest-framework.s3.amazonaws.com/zuplo.png)" > Zuplo< / a > < / li >
2016-06-24 13:05:19 +03:00
< / ul >
< div style = "clear: both; padding-bottom: 20px;" > < / div >
2024-07-05 18:56:58 +03:00
*Many thanks to all our [wonderful sponsors][sponsors], and in particular to our premium backers, [Sentry ](https://getsentry.com/welcome/ ), [Stream ](https://getstream.io/?utm_source=DjangoRESTFramework&utm_medium=Webpage_Logo_Ad&utm_content=Developer&utm_campaign=DjangoRESTFramework_Jan2022_HomePage ), [Spacinov ](https://www.spacinov.com/ ), [Retool ](https://retool.com/?utm_source=djangorest&utm_medium=sponsorship ), [bit.io ](https://bit.io/jobs?utm_source=DRF&utm_medium=sponsor&utm_campaign=DRF_sponsorship ), [PostHog ](https://posthog.com?utm_source=DRF&utm_medium=sponsor&utm_campaign=DRF_sponsorship ), [CryptAPI ](https://cryptapi.io ), [FEZTO ](https://www.fezto.xyz/?utm_source=DjangoRESTFramework ), [Svix ](https://www.svix.com/?utm_source=django-REST&utm_medium=sponsorship ), , and [Zuplo ](https://zuplo.link/django-web ).*
2012-10-30 16:23:17 +04:00
2016-05-28 12:55:38 +03:00
---
2014-01-03 17:06:41 +04:00
2012-09-01 23:26:27 +04:00
## Requirements
REST framework requires the following:
2024-09-07 14:21:18 +03:00
* Django (4.2, 5.0, 5.1)
2024-04-26 17:49:58 +03:00
* Python (3.8, 3.9, 3.10, 3.11, 3.12)
2012-09-01 23:26:27 +04:00
2018-12-20 16:41:54 +03:00
We **highly recommend** and only officially support the latest patch release of
each Python and Django series.
2012-09-01 23:26:27 +04:00
The following packages are optional:
2020-04-06 16:54:30 +03:00
* [PyYAML][pyyaml], [uritemplate][uriteemplate] (5.1+, 3.0.0+) - Schema generation support.
2024-04-26 18:09:08 +03:00
* [Markdown][markdown] (3.3.0+) - Markdown support for the browsable API.
* [Pygments][pygments] (2.7.0+) - Add syntax highlighting to Markdown processing.
2017-03-02 00:27:23 +03:00
* [django-filter][django-filter] (1.0.1+) - Filtering support.
2013-09-06 20:01:31 +04:00
* [django-guardian][django-guardian] (1.1.1+) - Object level permissions support.
2013-03-07 13:01:53 +04:00
2012-09-01 23:26:27 +04:00
## Installation
2012-09-03 15:11:57 +04:00
Install using `pip` , including any optional packages you want...
2012-09-01 23:26:27 +04:00
pip install djangorestframework
2013-05-05 21:12:35 +04:00
pip install markdown # Markdown support for the browsable API.
2012-11-17 00:58:49 +04:00
pip install django-filter # Filtering support
2012-09-01 23:26:27 +04:00
...or clone the project from github.
2018-12-19 21:28:03 +03:00
git clone https://github.com/encode/django-rest-framework
2012-09-01 23:26:27 +04:00
2012-12-27 21:53:07 +04:00
Add `'rest_framework'` to your `INSTALLED_APPS` setting.
2012-09-01 23:26:27 +04:00
2019-07-13 04:15:36 +03:00
INSTALLED_APPS = [
2012-09-01 23:26:27 +04:00
...
2014-01-14 18:40:36 +04:00
'rest_framework',
2019-07-13 04:15:36 +03:00
]
2012-09-01 23:26:27 +04:00
2013-05-02 08:26:40 +04:00
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
2012-09-01 23:26:27 +04:00
2014-09-24 01:08:38 +04:00
urlpatterns = [
2012-09-01 23:26:27 +04:00
...
2020-09-08 17:32:27 +03:00
path('api-auth/', include('rest_framework.urls'))
2014-09-24 01:08:38 +04:00
]
2012-09-20 16:06:27 +04:00
2017-11-20 11:35:54 +03:00
Note that the URL path can be whatever you want.
2012-09-20 16:06:27 +04:00
2013-04-25 01:40:24 +04:00
## Example
Let's take a look at a quick example of using REST framework to build a simple model-backed API.
2014-08-20 19:24:52 +04:00
We'll create a read-write API for accessing information on the users of our project.
2013-04-25 01:40:24 +04:00
2013-04-30 17:34:42 +04:00
Any global settings for a REST framework API are kept in a single configuration dictionary named `REST_FRAMEWORK` . Start off by adding the following to your `settings.py` module:
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS` .
We're ready to create our API now.
2013-04-30 11:24:33 +04:00
Here's our project's root `urls.py` module:
2013-04-29 16:20:15 +04:00
2020-05-11 13:01:00 +03:00
from django.urls import path, include
2014-08-20 19:24:52 +04:00
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
2018-07-06 12:28:18 +03:00
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
2019-07-13 04:15:36 +03:00
fields = ['url', 'username', 'email', 'is_staff']
2013-04-25 01:40:24 +04:00
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
2014-08-20 19:24:52 +04:00
queryset = User.objects.all()
serializer_class = UserSerializer
2014-01-14 18:40:36 +04:00
2013-12-13 19:30:59 +04:00
# Routers provide an easy way of automatically determining the URL conf.
2013-04-29 16:20:15 +04:00
router = routers.DefaultRouter()
2013-05-16 11:47:22 +04:00
router.register(r'users', UserViewSet)
2013-04-30 11:24:33 +04:00
2013-04-29 16:20:15 +04:00
# Wire up our API using automatic URL routing.
2014-11-29 22:23:55 +03:00
# Additionally, we include login URLs for the browsable API.
2014-08-20 19:24:52 +04:00
urlpatterns = [
2020-05-11 13:01:00 +03:00
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
2014-08-20 19:24:52 +04:00
]
2014-08-21 15:25:20 +04:00
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.
2013-04-25 01:40:24 +04:00
2012-09-01 23:26:27 +04:00
## Quickstart
2012-08-29 23:57:37 +04:00
2014-08-20 19:24:52 +04:00
Can't wait to get started? The [quickstart guide][quickstart] is the fastest way to get up and running, and building APIs with REST framework.
2012-10-05 13:23:47 +04:00
2012-09-02 00:35:30 +04:00
## Development
2014-03-02 15:40:30 +04:00
See the [Contribution guidelines][contributing] for information on how to clone
2024-04-26 16:13:37 +03:00
the repository, run the test suite and help maintain the code base of REST
2014-03-02 15:40:30 +04:00
Framework.
2013-03-06 16:19:49 +04:00
2012-10-09 15:01:17 +04:00
## Support
2022-10-19 13:20:11 +03:00
For support please see the [REST framework discussion group][group], try the `#restframework` channel on `irc.libera.chat` , or raise a question on [Stack Overflow][stack-overflow], making sure to include the ['django-rest-framework'][django-rest-framework-tag] tag.
2012-10-09 15:01:17 +04:00
2017-01-29 01:41:21 +03:00
For priority support please sign up for a [professional or premium sponsorship plan ](https://fund.django-rest-framework.org/topics/funding/ ).
2012-10-09 15:01:17 +04:00
2013-05-22 19:45:33 +04:00
## Security
2022-03-16 14:35:04 +03:00
Security issues are handled under the supervision of the [Django security team ](https://www.djangoproject.com/foundation/teams/#security-team ).
2013-05-22 19:45:33 +04:00
2022-03-16 14:35:04 +03:00
**Please report security issues by emailing security@djangoproject.com**.
The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.
2013-05-22 19:45:33 +04:00
2012-09-01 23:26:27 +04:00
## License
2018-10-02 13:12:31 +03:00
Copyright © 2011-present, [Encode OSS Ltd ](https://www.encode.io/ ).
2012-09-01 23:26:27 +04:00
All rights reserved.
2014-01-14 18:40:36 +04:00
Redistribution and use in source and binary forms, with or without
2012-09-01 23:26:27 +04:00
modification, are permitted provided that the following conditions are met:
2012-08-29 23:57:37 +04:00
2018-10-02 13:12:31 +03:00
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
2012-09-01 23:26:27 +04:00
2014-01-14 18:40:36 +04:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2012-09-01 23:26:27 +04:00
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2018-01-08 18:22:32 +03:00
[mozilla]: https://www.mozilla.org/en-US/about/
2016-06-24 13:05:19 +03:00
[redhat]: https://www.redhat.com/
[heroku]: https://www.heroku.com/
2014-01-03 17:06:41 +04:00
[eventbrite]: https://www.eventbrite.co.uk/about/
2020-04-06 16:54:30 +03:00
[pyyaml]: https://pypi.org/project/PyYAML/
[uriteemplate]: https://pypi.org/project/uritemplate/
2018-04-19 09:36:18 +03:00
[markdown]: https://pypi.org/project/Markdown/
2019-06-01 00:11:58 +03:00
[pygments]: https://pypi.org/project/Pygments/
2018-04-19 09:36:18 +03:00
[django-filter]: https://pypi.org/project/django-filter/
2017-04-09 23:21:42 +03:00
[django-guardian]: https://github.com/django-guardian/django-guardian
2013-06-27 16:41:42 +04:00
[index]: .
2015-01-05 15:26:15 +03:00
[oauth1-section]: api-guide/authentication/#django-rest-framework-oauth
[oauth2-section]: api-guide/authentication/#django-oauth-toolkit
2013-11-19 15:01:27 +04:00
[serializer-section]: api-guide/serializers#serializers
[modelserializer-section]: api-guide/serializers#modelserializer
[functionview-section]: api-guide/views#function-based-views
2016-06-24 13:05:19 +03:00
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors
2012-08-29 23:57:37 +04:00
2012-10-09 15:01:17 +04:00
[quickstart]: tutorial/quickstart.md
2018-07-06 12:28:18 +03:00
2012-09-12 13:12:13 +04:00
[generic-views]: api-guide/generic-views.md
2013-04-09 14:54:51 +04:00
[viewsets]: api-guide/viewsets.md
[routers]: api-guide/routers.md
2012-09-01 23:26:27 +04:00
[serializers]: api-guide/serializers.md
[authentication]: api-guide/authentication.md
2018-07-06 12:28:18 +03:00
[contributing]: community/contributing.md
[funding]: community/funding.md
2012-10-09 15:01:17 +04:00
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
2018-01-08 18:22:32 +03:00
[stack-overflow]: https://stackoverflow.com/
[django-rest-framework-tag]: https://stackoverflow.com/questions/tagged/django-rest-framework
2013-05-22 19:45:33 +04:00
[security-mail]: mailto:rest-framework-security@googlegroups.com
2013-01-24 17:01:42 +04:00
[twitter]: https://twitter.com/_tomchristie