| 1 |
--- |
| 2 |
hide: |
| 3 |
- navigation |
| 4 |
--- |
| 5 |
|
| 6 |
<style> |
| 7 |
.promo li a { |
| 8 |
float: left; |
| 9 |
width: 130px; |
| 10 |
height: 20px; |
| 11 |
text-align: center; |
| 12 |
margin: 10px 30px; |
| 13 |
padding: 150px 0 0 0; |
| 14 |
background-position: 0 50%; |
| 15 |
background-size: 130px auto; |
| 16 |
background-repeat: no-repeat; |
| 17 |
font-size: 120%; |
| 18 |
color: black; |
| 19 |
} |
| 20 |
.promo li { |
| 21 |
list-style: none; |
| 22 |
} |
| 23 |
</style> |
| 24 |
|
| 25 |
--- |
| 26 |
|
| 27 |
<h1 style="position: absolute; |
| 28 |
width: 1px; |
| 29 |
height: 1px; |
| 30 |
padding: 0; |
| 31 |
margin: -1px; |
| 32 |
overflow: hidden; |
| 33 |
clip: rect(0,0,0,0); |
| 34 |
border: 0;">Django REST Framework</h1> |
| 35 |
|
| 36 |
 |
| 37 |
 |
| 38 |
|
| 39 |
Django REST framework is a powerful and flexible toolkit for building Web APIs. |
| 40 |
|
| 41 |
Some reasons you might want to use REST framework: |
| 42 |
|
| 43 |
* The Web browsable API is a huge usability win for your developers. |
| 44 |
* [Authentication policies][authentication] including packages for [OAuth1a][oauth1-section] and [OAuth2][oauth2-section]. |
| 45 |
* [Serialization][serializers] that supports both [ORM][modelserializer-section] and [non-ORM][serializer-section] data sources. |
| 46 |
* 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]. |
| 47 |
* Extensive documentation, and [great community support][group]. |
| 48 |
* Used and trusted by internationally recognized companies including [Mozilla][mozilla], [Red Hat][redhat], [Heroku][heroku], and [Eventbrite][eventbrite]. |
| 49 |
|
| 50 |
--- |
| 51 |
|
| 52 |
## Requirements |
| 53 |
|
| 54 |
REST framework requires the following: |
| 55 |
|
| 56 |
* Django (5.2, 6.0, 6.1) |
| 57 |
* Python (3.10, 3.11, 3.12, 3.13, 3.14) |
| 58 |
|
| 59 |
We **highly recommend** and only officially support the latest patch release of |
| 60 |
each Python and Django series. |
| 61 |
|
| 62 |
The following packages are optional: |
| 63 |
|
| 64 |
* [PyYAML][pyyaml], [uritemplate][uritemplate] (5.1+, 3.0.0+) - Schema generation support. |
| 65 |
* [Markdown][markdown] (3.3.0+) - Markdown support for the browsable API. |
| 66 |
* [Pygments][pygments] (2.7.0+) - Add syntax highlighting to Markdown processing. |
| 67 |
* [django-filter][django-filter] (1.0.1+) - Filtering support. |
| 68 |
* [django-guardian][django-guardian] (1.1.1+) - Object level permissions support. |
| 69 |
|
| 70 |
## Installation |
| 71 |
|
| 72 |
Install using `pip`, including any optional packages you want... |
| 73 |
|
| 74 |
```bash |
| 75 |
pip install djangorestframework |
| 76 |
pip install markdown # Markdown support for the browsable API. |
| 77 |
pip install django-filter # Filtering support |
| 78 |
``` |
| 79 |
|
| 80 |
...or clone the project from github. |
| 81 |
|
| 82 |
```bash |
| 83 |
git clone https://github.com/encode/django-rest-framework |
| 84 |
``` |
| 85 |
|
| 86 |
Add `'rest_framework'` to your `INSTALLED_APPS` setting. |
| 87 |
|
| 88 |
```python |
| 89 |
INSTALLED_APPS = [ |
| 90 |
# ... |
| 91 |
"rest_framework", |
| 92 |
] |
| 93 |
``` |
| 94 |
|
| 95 |
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. |
| 96 |
|
| 97 |
```python |
| 98 |
urlpatterns = [ |
| 99 |
# ... |
| 100 |
path("api-auth/", include("rest_framework.urls")) |
| 101 |
] |
| 102 |
``` |
| 103 |
|
| 104 |
Note that the URL path can be whatever you want. |
| 105 |
|
| 106 |
## Example |
| 107 |
|
| 108 |
Let's take a look at a quick example of using REST framework to build a simple model-backed API. |
| 109 |
|
| 110 |
We'll create a read-write API for accessing information on the users of our project. |
| 111 |
|
| 112 |
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: |
| 113 |
|
| 114 |
```python |
| 115 |
REST_FRAMEWORK = { |
| 116 |
# Use Django's standard `django.contrib.auth` permissions, |
| 117 |
# or allow read-only access for unauthenticated users. |
| 118 |
"DEFAULT_PERMISSION_CLASSES": [ |
| 119 |
"rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly" |
| 120 |
] |
| 121 |
} |
| 122 |
``` |
| 123 |
|
| 124 |
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS`. |
| 125 |
|
| 126 |
We're ready to create our API now. |
| 127 |
Here's our project's root `urls.py` module: |
| 128 |
|
| 129 |
```python |
| 130 |
from django.urls import path, include |
| 131 |
from django.contrib.auth.models import User |
| 132 |
from rest_framework import routers, serializers, viewsets |
| 133 |
|
| 134 |
|
| 135 |
# Serializers define the API representation. |
| 136 |
class UserSerializer(serializers.HyperlinkedModelSerializer): |
| 137 |
class Meta: |
| 138 |
model = User |
| 139 |
fields = ["url", "username", "email", "is_staff"] |
| 140 |
|
| 141 |
|
| 142 |
# ViewSets define the view behavior. |
| 143 |
class UserViewSet(viewsets.ModelViewSet): |
| 144 |
queryset = User.objects.all() |
| 145 |
serializer_class = UserSerializer |
| 146 |
|
| 147 |
|
| 148 |
# Routers provide an easy way of automatically determining the URL conf. |
| 149 |
router = routers.DefaultRouter() |
| 150 |
router.register(r"users", UserViewSet) |
| 151 |
|
| 152 |
# Wire up our API using automatic URL routing. |
| 153 |
# Additionally, we include login URLs for the browsable API. |
| 154 |
urlpatterns = [ |
| 155 |
path("", include(router.urls)), |
| 156 |
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), |
| 157 |
] |
| 158 |
``` |
| 159 |
|
| 160 |
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. |
| 161 |
|
| 162 |
## Quickstart |
| 163 |
|
| 164 |
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. |
| 165 |
|
| 166 |
## Development |
| 167 |
|
| 168 |
See the [Contribution guidelines][contributing] for information on how to clone |
| 169 |
the repository, run the test suite and help maintain the code base of REST |
| 170 |
Framework. |
| 171 |
|
| 172 |
## Support |
| 173 |
|
| 174 |
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. |
| 175 |
|
| 176 |
## Security |
| 177 |
|
| 178 |
**Please report security issues by emailing [email protected]**. |
| 179 |
|
| 180 |
The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure. |
| 181 |
|
| 182 |
## License |
| 183 |
|
| 184 |
Copyright © 2011-present, [Encode OSS Ltd](https://www.encode.io/). |
| 185 |
All rights reserved. |
| 186 |
|
| 187 |
Redistribution and use in source and binary forms, with or without |
| 188 |
modification, are permitted provided that the following conditions are met: |
| 189 |
|
| 190 |
* Redistributions of source code must retain the above copyright notice, this |
| 191 |
list of conditions and the following disclaimer. |
| 192 |
|
| 193 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 194 |
this list of conditions and the following disclaimer in the documentation |
| 195 |
and/or other materials provided with the distribution. |
| 196 |
|
| 197 |
* Neither the name of the copyright holder nor the names of its |
| 198 |
contributors may be used to endorse or promote products derived from |
| 199 |
this software without specific prior written permission. |
| 200 |
|
| 201 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 202 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 203 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 204 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 205 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 206 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 207 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 208 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 209 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 210 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 211 |
|
| 212 |
[mozilla]: https://www.mozilla.org/en-US/about/ |
| 213 |
[redhat]: https://www.redhat.com/ |
| 214 |
[heroku]: https://www.heroku.com/ |
| 215 |
[eventbrite]: https://www.eventbrite.co.uk/about/ |
| 216 |
[pyyaml]: https://pypi.org/project/PyYAML/ |
| 217 |
[uritemplate]: https://pypi.org/project/uritemplate/ |
| 218 |
[markdown]: https://pypi.org/project/Markdown/ |
| 219 |
[pygments]: https://pypi.org/project/Pygments/ |
| 220 |
[django-filter]: https://pypi.org/project/django-filter/ |
| 221 |
[django-guardian]: https://github.com/django-guardian/django-guardian |
| 222 |
[index]: . |
| 223 |
[oauth1-section]: api-guide/authentication/#django-rest-framework-oauth |
| 224 |
[oauth2-section]: api-guide/authentication/#django-oauth-toolkit |
| 225 |
[serializer-section]: api-guide/serializers#serializers |
| 226 |
[modelserializer-section]: api-guide/serializers#modelserializer |
| 227 |
[functionview-section]: api-guide/views#function-based-views |
| 228 |
|
| 229 |
[quickstart]: tutorial/quickstart.md |
| 230 |
|
| 231 |
[generic-views]: api-guide/generic-views.md |
| 232 |
[viewsets]: api-guide/viewsets.md |
| 233 |
[routers]: api-guide/routers.md |
| 234 |
[serializers]: api-guide/serializers.md |
| 235 |
[authentication]: api-guide/authentication.md |
| 236 |
|
| 237 |
[contributing]: community/contributing.md |
| 238 |
|
| 239 |
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework |
| 240 |
[stack-overflow]: https://stackoverflow.com/ |
| 241 |
[django-rest-framework-tag]: https://stackoverflow.com/questions/tagged/django-rest-framework |
| 242 |
[security-mail]: mailto:[email protected] |