| 1 |
# [Django REST framework][docs] |
| 2 |
|
| 3 |
[![build-status-image]][build-status] |
| 4 |
[![coverage-status-image]][codecov] |
| 5 |
[![pypi-version]][pypi] |
| 6 |
|
| 7 |
**Awesome web-browsable Web APIs.** |
| 8 |
|
| 9 |
Full documentation for the project is available at [https://www.django-rest-framework.org/][docs]. |
| 10 |
|
| 11 |
--- |
| 12 |
|
| 13 |
# Overview |
| 14 |
|
| 15 |
Django REST framework is a powerful and flexible toolkit for building Web APIs. |
| 16 |
|
| 17 |
Some reasons you might want to use REST framework: |
| 18 |
|
| 19 |
* The Web browsable API is a huge usability win for your developers. |
| 20 |
* [Authentication policies][authentication] including optional packages for [OAuth1a][oauth1-section] and [OAuth2][oauth2-section]. |
| 21 |
* [Serialization][serializers] that supports both [ORM][modelserializer-section] and [non-ORM][serializer-section] data sources. |
| 22 |
* 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]. |
| 23 |
* [Extensive documentation][docs], and [great community support][group]. |
| 24 |
|
| 25 |
**Below**: *Screenshot from the browsable API* |
| 26 |
|
| 27 |
![Screenshot][image] |
| 28 |
|
| 29 |
---- |
| 30 |
|
| 31 |
# Requirements |
| 32 |
|
| 33 |
* Python 3.10+ |
| 34 |
* Django 5.2, 6.0, 6.1 |
| 35 |
|
| 36 |
We **highly recommend** and only officially support the latest patch release of |
| 37 |
each Python and Django series. |
| 38 |
|
| 39 |
# Installation |
| 40 |
|
| 41 |
Install using `pip`... |
| 42 |
|
| 43 |
pip install djangorestframework |
| 44 |
|
| 45 |
Add `'rest_framework'` to your `INSTALLED_APPS` setting. |
| 46 |
|
| 47 |
```python |
| 48 |
INSTALLED_APPS = [ |
| 49 |
# ... |
| 50 |
"rest_framework", |
| 51 |
] |
| 52 |
``` |
| 53 |
|
| 54 |
# Example |
| 55 |
|
| 56 |
Let's take a look at a quick example of using REST framework to build a simple model-backed API for accessing users and groups. |
| 57 |
|
| 58 |
Start up a new project like so... |
| 59 |
|
| 60 |
pip install django |
| 61 |
pip install djangorestframework |
| 62 |
django-admin startproject example . |
| 63 |
./manage.py migrate |
| 64 |
./manage.py createsuperuser |
| 65 |
|
| 66 |
|
| 67 |
Now edit the `example/urls.py` module in your project: |
| 68 |
|
| 69 |
```python |
| 70 |
from django.contrib.auth.models import User |
| 71 |
from django.urls import include, path |
| 72 |
from rest_framework import routers, serializers, viewsets |
| 73 |
|
| 74 |
|
| 75 |
# Serializers define the API representation. |
| 76 |
class UserSerializer(serializers.HyperlinkedModelSerializer): |
| 77 |
class Meta: |
| 78 |
model = User |
| 79 |
fields = ["url", "username", "email", "is_staff"] |
| 80 |
|
| 81 |
|
| 82 |
# ViewSets define the view behavior. |
| 83 |
class UserViewSet(viewsets.ModelViewSet): |
| 84 |
queryset = User.objects.all() |
| 85 |
serializer_class = UserSerializer |
| 86 |
|
| 87 |
|
| 88 |
# Routers provide a way of automatically determining the URL conf. |
| 89 |
router = routers.DefaultRouter() |
| 90 |
router.register(r"users", UserViewSet) |
| 91 |
|
| 92 |
# Wire up our API using automatic URL routing. |
| 93 |
# Additionally, we include login URLs for the browsable API. |
| 94 |
urlpatterns = [ |
| 95 |
path("", include(router.urls)), |
| 96 |
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), |
| 97 |
] |
| 98 |
``` |
| 99 |
|
| 100 |
We'd also like to configure a couple of settings for our API. |
| 101 |
|
| 102 |
Add the following to your `settings.py` module: |
| 103 |
|
| 104 |
```python |
| 105 |
INSTALLED_APPS = [ |
| 106 |
# ... make sure to include the default installed apps here. |
| 107 |
"rest_framework", |
| 108 |
] |
| 109 |
|
| 110 |
REST_FRAMEWORK = { |
| 111 |
# Use Django's standard `django.contrib.auth` permissions, |
| 112 |
# or allow read-only access for unauthenticated users. |
| 113 |
"DEFAULT_PERMISSION_CLASSES": [ |
| 114 |
"rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly", |
| 115 |
] |
| 116 |
} |
| 117 |
``` |
| 118 |
|
| 119 |
That's it, we're done! |
| 120 |
|
| 121 |
./manage.py runserver |
| 122 |
|
| 123 |
You can now open the API in your browser at `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. |
| 124 |
|
| 125 |
You can also interact with the API using command line tools such as [`curl`](https://curl.haxx.se/). For example, to list the users endpoint: |
| 126 |
|
| 127 |
$ curl -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/users/ |
| 128 |
[ |
| 129 |
{ |
| 130 |
"url": "http://127.0.0.1:8000/users/1/", |
| 131 |
"username": "admin", |
| 132 |
"email": "[email protected]", |
| 133 |
"is_staff": true, |
| 134 |
} |
| 135 |
] |
| 136 |
|
| 137 |
Or to create a new user: |
| 138 |
|
| 139 |
$ curl -X POST -d username=new -d [email protected] -d is_staff=false -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/users/ |
| 140 |
{ |
| 141 |
"url": "http://127.0.0.1:8000/users/2/", |
| 142 |
"username": "new", |
| 143 |
"email": "[email protected]", |
| 144 |
"is_staff": false, |
| 145 |
} |
| 146 |
|
| 147 |
# Documentation & Support |
| 148 |
|
| 149 |
Full documentation for the project is available at [https://www.django-rest-framework.org/][docs]. |
| 150 |
|
| 151 |
For questions and support, use the [REST framework discussion group][group], or `#restframework` on libera.chat IRC. |
| 152 |
|
| 153 |
# Security |
| 154 |
|
| 155 |
Please see the [security policy][security-policy]. |
| 156 |
|
| 157 |
[build-status-image]: https://github.com/encode/django-rest-framework/actions/workflows/main.yml/badge.svg |
| 158 |
[build-status]: https://github.com/encode/django-rest-framework/actions/workflows/main.yml |
| 159 |
[coverage-status-image]: https://img.shields.io/codecov/c/github/encode/django-rest-framework/main.svg |
| 160 |
[codecov]: https://codecov.io/github/encode/django-rest-framework?branch=main |
| 161 |
[pypi-version]: https://img.shields.io/pypi/v/djangorestframework.svg |
| 162 |
[pypi]: https://pypi.org/project/djangorestframework/ |
| 163 |
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework |
| 164 |
|
| 165 |
[funding]: https://fund.django-rest-framework.org/topics/funding/ |
| 166 |
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors |
| 167 |
|
| 168 |
[oauth1-section]: https://www.django-rest-framework.org/api-guide/authentication/#django-rest-framework-oauth |
| 169 |
[oauth2-section]: https://www.django-rest-framework.org/api-guide/authentication/#django-oauth-toolkit |
| 170 |
[serializer-section]: https://www.django-rest-framework.org/api-guide/serializers/#serializers |
| 171 |
[modelserializer-section]: https://www.django-rest-framework.org/api-guide/serializers/#modelserializer |
| 172 |
[functionview-section]: https://www.django-rest-framework.org/api-guide/views/#function-based-views |
| 173 |
[generic-views]: https://www.django-rest-framework.org/api-guide/generic-views/ |
| 174 |
[viewsets]: https://www.django-rest-framework.org/api-guide/viewsets/ |
| 175 |
[routers]: https://www.django-rest-framework.org/api-guide/routers/ |
| 176 |
[serializers]: https://www.django-rest-framework.org/api-guide/serializers/ |
| 177 |
[authentication]: https://www.django-rest-framework.org/api-guide/authentication/ |
| 178 |
[image]: https://www.django-rest-framework.org/img/quickstart.png |
| 179 |
|
| 180 |
[docs]: https://www.django-rest-framework.org/ |
| 181 |
[security-policy]: https://github.com/encode/django-rest-framework/security/policy |