2014-12-09 13:06:23 +03:00
# [Django REST framework][docs]
2014-07-18 16:20:03 +04:00
2021-04-12 15:14:26 +03:00
[![build-status-image]][build-status]
2015-08-03 18:57:18 +03:00
[![coverage-status-image]][codecov]
2014-11-13 15:06:45 +03:00
[![pypi-version]][pypi]
2014-07-18 16:20:03 +04:00
2014-11-29 22:23:55 +03:00
**Awesome web-browsable Web APIs.**
2014-07-18 16:20:03 +04:00
2018-10-02 09:28:58 +03:00
Full documentation for the project is available at [https://www.django-rest-framework.org/][docs].
2014-11-03 17:43:00 +03:00
---
2016-06-23 15:29:38 +03:00
# Funding
REST framework is a *collaboratively funded project* . If you use
REST framework commercially we strongly encourage you to invest in its
2017-11-27 13:32:17 +03:00
continued development by [signing up for a paid plan][funding].
2016-06-23 15:29:38 +03:00
The initial aim is to provide a single full-time position on REST framework.
2016-08-11 18:53:34 +03:00
*Every single sign-up makes a significant impact towards making that possible.*
2016-06-23 15:29:38 +03:00
2017-11-27 13:32:17 +03:00
[![][sentry-img]][sentry-url]
[![][stream-img]][stream-url]
2022-06-06 13:07:25 +03:00
[![][spacinov-img]][spacinov-url]
2019-10-07 16:21:58 +03:00
[![][retool-img]][retool-url]
2020-07-29 16:55:10 +03:00
[![][bitio-img]][bitio-url]
2021-10-05 16:02:34 +03:00
[![][posthog-img]][posthog-url]
2021-12-08 17:53:06 +03:00
[![][cryptapi-img]][cryptapi-url]
2022-05-26 12:40:46 +03:00
[![][fezto-img]][fezto-url]
2023-11-29 17:13:36 +03:00
[![][svix-img]][svix-url]
2024-07-05 18:56:58 +03:00
[![][zuplo-img]][zuplo-url]
2016-06-23 15:29:38 +03:00
2024-07-05 18:56:58 +03:00
Many thanks to all our [wonderful sponsors][sponsors], and in particular to our premium backers, [Sentry][sentry-url], [Stream][stream-url], [Spacinov][spacinov-url], [Retool][retool-url], [bit.io][bitio-url], [PostHog][posthog-url], [CryptAPI][cryptapi-url], [FEZTO][fezto-url], [Svix][svix-url], and [Zuplo][zuplo-url].
2014-11-03 17:43:00 +03:00
---
2014-07-18 16:20:03 +04:00
2012-08-29 23:57:37 +04:00
# Overview
2014-08-18 23:51:08 +04:00
Django REST framework is a powerful and flexible toolkit for building Web APIs.
2012-10-30 16:23:17 +04:00
2013-04-30 12:32:11 +04:00
Some reasons you might want to use REST framework:
2012-08-29 23:57:37 +04:00
2023-11-30 14:07:36 +03:00
* The Web browsable API is a huge usability win for your developers.
2014-12-12 18:37:43 +03:00
* [Authentication policies][authentication] including optional 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].
2015-03-07 17:50:31 +03:00
* [Extensive documentation][docs], and [great community support][group].
2012-10-30 16:23:17 +04:00
2014-11-29 22:23:55 +03:00
**Below**: *Screenshot from the browsable API*
2013-04-30 11:33:33 +04:00
![Screenshot][image]
2017-11-27 13:32:17 +03:00
----
2012-08-29 23:57:37 +04:00
# Requirements
2024-04-26 17:49:58 +03:00
* Python 3.8+
2024-09-07 14:21:18 +03:00
* Django 4.2, 5.0, 5.1
2012-08-29 23:57:37 +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-08-29 23:57:37 +04:00
# Installation
2013-04-30 11:33:33 +04:00
Install using `pip` ...
2012-08-29 23:57:37 +04:00
2012-10-26 22:55:23 +04:00
pip install djangorestframework
2012-12-27 21:53:07 +04:00
Add `'rest_framework'` to your `INSTALLED_APPS` setting.
2022-03-16 14:35:24 +03:00
```python
INSTALLED_APPS = [
...
'rest_framework',
]
```
2012-12-27 21:53:07 +04:00
2024-09-26 00:45:55 +03:00
# Getting Started
To help you get started quickly with Django REST framework, follow these steps to create a simple API project.
### Step 1: Install Django and Django REST framework
First, make sure you have Python installed on your machine. You can install Django and Django REST framework using pip:
```bash
pip install django djangorestframework
```
### Step 2: Create a New Django Project
Create a new Django project by running the following command
```bash
django-admin startproject myproject
cd myproject
```
### Step 3: Create a New Django App
Next, create a new app within your project:
```bash
python manage.py startapp myapp
```
### Step 4: Update Settings
Add your new app and REST framework to the INSTALLED_APPS list in myproject/settings.py:
```bash
INSTALLED_APPS = [
...
'rest_framework',
'myapp',
]
```
Once you have follow the steps above, you should be able to use the REST framework. View the example below for extra details.
2013-04-30 11:33:33 +04:00
# Example
2013-05-02 15:08:05 +04:00
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.
2013-04-30 17:34:42 +04:00
2014-11-29 21:50:51 +03:00
Startup up a new project like so...
2014-08-20 19:24:52 +04:00
pip install django
pip install djangorestframework
2018-10-03 18:16:52 +03:00
django-admin startproject example .
2016-02-03 13:01:37 +03:00
./manage.py migrate
./manage.py createsuperuser
2014-08-20 19:24:52 +04:00
Now edit the `example/urls.py` module in your project:
2012-09-02 00:35:30 +04:00
2013-11-18 11:21:09 +04:00
```python
2014-08-20 19:24:52 +04:00
from django.contrib.auth.models import User
2022-08-30 14:30:42 +03:00
from django.urls import include, path
from rest_framework import routers, serializers, viewsets
2014-08-20 19:24:52 +04: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']
2014-08-20 19:24:52 +04:00
2012-09-02 00:35:30 +04:00
2013-11-18 11:21:09 +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
2013-04-30 11:33:33 +04:00
2014-11-29 21:50:51 +03:00
2014-08-20 19:24:52 +04:00
# Routers provide a way of automatically determining the URL conf.
2013-11-18 11:21:09 +04:00
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
2013-04-30 11:33:33 +04:00
2013-11-18 11:21:09 +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)),
2021-03-09 13:16:19 +03:00
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
2014-08-20 19:24:52 +04:00
]
2013-11-18 11:21:09 +04:00
```
2013-03-06 16:29:32 +04:00
2013-05-02 15:08:05 +04:00
We'd also like to configure a couple of settings for our API.
Add the following to your `settings.py` module:
2013-11-18 11:21:09 +04:00
```python
2019-07-13 04:15:36 +03:00
INSTALLED_APPS = [
2014-08-20 19:24:52 +04:00
... # Make sure to include the default installed apps here.
2014-11-29 21:50:51 +03:00
'rest_framework',
2019-07-13 04:15:36 +03:00
]
2013-11-18 11:21:09 +04:00
2014-08-20 19:24:52 +04:00
REST_FRAMEWORK = {
2013-11-18 11:21:09 +04:00
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
2021-03-09 13:16:19 +03:00
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
2013-11-18 11:21:09 +04:00
]
}
```
2013-05-02 15:08:05 +04:00
That's it, we're done!
2014-08-20 19:24:52 +04:00
./manage.py runserver
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.
2018-01-08 18:22:32 +03:00
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:
2014-08-20 19:24:52 +04:00
$ curl -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/users/
2018-09-11 07:44:23 +03:00
[
{
"url": "http://127.0.0.1:8000/users/1/",
"username": "admin",
"email": "admin@example.com",
"is_staff": true,
}
]
2014-08-20 19:24:52 +04:00
Or to create a new user:
$ curl -X POST -d username=new -d email=new@example.com -d is_staff=false -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/users/
{
2014-11-29 21:50:51 +03:00
"url": "http://127.0.0.1:8000/users/2/",
"username": "new",
"email": "new@example.com",
"is_staff": false,
2014-08-20 19:24:52 +04:00
}
2024-09-26 00:45:55 +03:00
# Common Issues and Solutions
### 1. Database Migrations
If you encounter issues related to database migrations, ensure that you have run:
```bash
python manage.py makemigrations
python manage.py migrate
```
### 2. Missing Packages
If you receive errors about missing packages, double-check that you have installed both Django and Django REST framework using pip.
### 3. Debugging Configuration Errors
For configuration issues, ensure that your settings.py file includes all necessary apps in the INSTALLED_APPS list, and that your URLs are correctly configured.
For more detailed troubleshooting, refer to the documentation provided below.
2013-04-30 11:40:52 +04:00
# Documentation & Support
2013-03-06 16:29:32 +04:00
2018-10-02 09:28:58 +03:00
Full documentation for the project is available at [https://www.django-rest-framework.org/][docs].
2013-04-30 11:40:52 +04:00
2021-08-06 18:46:26 +03:00
For questions and support, use the [REST framework discussion group][group], or `#restframework` on libera.chat IRC.
2013-04-30 11:40:52 +04:00
2013-05-22 19:45:33 +04:00
# Security
2019-05-29 11:57:07 +03:00
Please see the [security policy][security-policy].
2013-05-22 19:45:33 +04:00
2021-04-12 15:14:26 +03:00
[build-status-image]: https://github.com/encode/django-rest-framework/actions/workflows/main.yml/badge.svg
[build-status]: https://github.com/encode/django-rest-framework/actions/workflows/main.yml
2017-04-07 17:28:35 +03:00
[coverage-status-image]: https://img.shields.io/codecov/c/github/encode/django-rest-framework/master.svg
2018-01-08 18:22:32 +03:00
[codecov]: https://codecov.io/github/encode/django-rest-framework?branch=master
2015-05-27 17:47:44 +03:00
[pypi-version]: https://img.shields.io/pypi/v/djangorestframework.svg
2018-04-19 09:36:18 +03:00
[pypi]: https://pypi.org/project/djangorestframework/
2012-12-18 02:00:25 +04:00
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
2013-06-27 16:41:42 +04:00
2016-06-23 15:29:38 +03:00
[funding]: https://fund.django-rest-framework.org/topics/funding/
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors
2017-11-27 13:32:17 +03:00
[sentry-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/sentry-readme.png
[stream-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/stream-readme.png
2022-06-06 13:07:25 +03:00
[spacinov-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/spacinov-readme.png
2019-10-07 16:21:58 +03:00
[retool-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/retool-readme.png
2020-07-29 16:55:10 +03:00
[bitio-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/bitio-readme.png
2021-10-05 16:02:34 +03:00
[posthog-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/posthog-readme.png
2021-12-08 17:53:45 +03:00
[cryptapi-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/cryptapi-readme.png
2022-05-26 12:40:46 +03:00
[fezto-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/fezto-readme.png
2023-11-29 17:15:53 +03:00
[svix-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/svix-premium.png
2024-07-05 18:56:58 +03:00
[zuplo-img]: https://raw.githubusercontent.com/encode/django-rest-framework/master/docs/img/premium/zuplo-readme.png
2017-11-27 13:32:17 +03:00
[sentry-url]: https://getsentry.com/welcome/
2022-01-11 16:01:25 +03:00
[stream-url]: https://getstream.io/?utm_source=DjangoRESTFramework& utm_medium=Webpage_Logo_Ad& utm_content=Developer& utm_campaign=DjangoRESTFramework_Jan2022_HomePage
2022-06-06 13:07:25 +03:00
[spacinov-url]: https://www.spacinov.com/
2019-10-07 16:22:40 +03:00
[retool-url]: https://retool.com/?utm_source=djangorest& utm_medium=sponsorship
2020-07-29 16:55:10 +03:00
[bitio-url]: https://bit.io/jobs?utm_source=DRF& utm_medium=sponsor& utm_campaign=DRF_sponsorship
2021-10-05 16:02:34 +03:00
[posthog-url]: https://posthog.com?utm_source=drf& utm_medium=sponsorship& utm_campaign=open-source-sponsorship
2021-12-08 17:53:06 +03:00
[cryptapi-url]: https://cryptapi.io
2022-05-26 12:40:46 +03:00
[fezto-url]: https://www.fezto.xyz/?utm_source=DjangoRESTFramework
2023-11-29 17:13:36 +03:00
[svix-url]: https://www.svix.com/?utm_source=django-REST& utm_medium=sponsorship
2024-07-05 18:56:58 +03:00
[zuplo-url]: https://zuplo.link/django-gh
2017-11-27 13:32:17 +03:00
2018-10-02 09:28:58 +03:00
[oauth1-section]: https://www.django-rest-framework.org/api-guide/authentication/#django-rest-framework-oauth
[oauth2-section]: https://www.django-rest-framework.org/api-guide/authentication/#django-oauth-toolkit
[serializer-section]: https://www.django-rest-framework.org/api-guide/serializers/#serializers
[modelserializer-section]: https://www.django-rest-framework.org/api-guide/serializers/#modelserializer
[functionview-section]: https://www.django-rest-framework.org/api-guide/views/#function-based-views
[generic-views]: https://www.django-rest-framework.org/api-guide/generic-views/
[viewsets]: https://www.django-rest-framework.org/api-guide/viewsets/
[routers]: https://www.django-rest-framework.org/api-guide/routers/
[serializers]: https://www.django-rest-framework.org/api-guide/serializers/
[authentication]: https://www.django-rest-framework.org/api-guide/authentication/
[image]: https://www.django-rest-framework.org/img/quickstart.png
[docs]: https://www.django-rest-framework.org/
2019-05-29 11:57:07 +03:00
[security-policy]: https://github.com/encode/django-rest-framework/security/policy