Debian Codegraph @

docs/index.md

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