βΉοΈ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.django-rest-framework.org/tutorial/quickstart/ |
| Last Crawled | 2026-04-11 02:11:37 (1 day ago) |
| First Indexed | 2018-03-29 13:33:42 (8 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Quickstart - Django REST framework |
| Meta Description | Django REST framework - Web APIs for Django |
| Meta Canonical | null |
| Boilerpipe Text | We're going to create a simple API to allow admin users to view and edit the users and groups in the system.
Project setup
ΒΆ
Create a new Django project named
tutorial
, then start a new app called
quickstart
.
Linux,
macOS
Windows
# Create the project directory
mkdir
tutorial
cd
tutorial
# Create a virtual environment to isolate our package dependencies locally
python3
-m
venv
.venv
source
.venv/bin/activate
# Install Django and Django REST framework into the virtual environment
pip
install
djangorestframework
# Set up a new project with a single application
django-admin
startproject
tutorial
.
# Note the trailing '.' character
cd
tutorial
django-admin
startapp
quickstart
cd
..
If you use Bash for Windows
# Create the project directory
mkdir
tutorial
cd
tutorial
# Create a virtual environment to isolate our package dependencies locally
python3
-m
venv
.venv
source
.venv
\S
cripts
\a
ctivate
# Install Django and Django REST framework into the virtual environment
pip
install
djangorestframework
# Set up a new project with a single application
django-admin
startproject
tutorial
.
# Note the trailing '.' character
cd
tutorial
django-admin
startapp
quickstart
cd
..
The project layout should look like:
$
pwd
<some
path>/tutorial
$
find
.
.
./tutorial
./tutorial/asgi.py
./tutorial/__init__.py
./tutorial/quickstart
./tutorial/quickstart/migrations
./tutorial/quickstart/migrations/__init__.py
./tutorial/quickstart/models.py
./tutorial/quickstart/__init__.py
./tutorial/quickstart/apps.py
./tutorial/quickstart/admin.py
./tutorial/quickstart/tests.py
./tutorial/quickstart/views.py
./tutorial/settings.py
./tutorial/urls.py
./tutorial/wsgi.py
./env
./env/...
./manage.py
It may look unusual that the application has been created within the project directory. Using the project's namespace avoids name clashes with external modules (a topic that goes outside the scope of the quickstart).
Now sync your database for the first time:
python
manage.py
migrate
We'll also create an initial user named
admin
with a password. We'll authenticate as that user later in our example.
python
manage.py
createsuperuser
--username
admin
--email
admin@example.com
Once you've set up a database and the initial user is created and ready to go, open up the app's directory and we'll get coding...
Serializers
ΒΆ
First up we're going to define some serializers. Let's create a new module named
tutorial/quickstart/serializers.py
that we'll use for our data representations.
from
django.contrib.auth.models
import
Group
,
User
from
rest_framework
import
serializers
class
UserSerializer
(
serializers
.
HyperlinkedModelSerializer
):
class
Meta
:
model
=
User
fields
=
[
"url"
,
"username"
,
"email"
,
"groups"
]
class
GroupSerializer
(
serializers
.
HyperlinkedModelSerializer
):
class
Meta
:
model
=
Group
fields
=
[
"url"
,
"name"
]
Notice that we're using hyperlinked relations in this case with
HyperlinkedModelSerializer
. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
Views
ΒΆ
Right, we'd better write some views then. Open
tutorial/quickstart/views.py
and get typing.
from
django.contrib.auth.models
import
Group
,
User
from
rest_framework
import
permissions
,
viewsets
from
tutorial.quickstart.serializers
import
GroupSerializer
,
UserSerializer
class
UserViewSet
(
viewsets
.
ModelViewSet
):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset
=
User
.
objects
.
all
()
.
order_by
(
"-date_joined"
)
serializer_class
=
UserSerializer
permission_classes
=
[
permissions
.
IsAuthenticated
]
class
GroupViewSet
(
viewsets
.
ModelViewSet
):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset
=
Group
.
objects
.
all
()
.
order_by
(
"name"
)
serializer_class
=
GroupSerializer
permission_classes
=
[
permissions
.
IsAuthenticated
]
Rather than write multiple views we're grouping together all the common behavior into classes called
ViewSets
.
We can easily break these down into individual views if we need to, but using viewsets keeps the view logic nicely organized as well as being very concise.
URLs
ΒΆ
Okay, now let's wire up the API URLs. On to
tutorial/urls.py
...
from
django.urls
import
include
,
path
from
rest_framework
import
routers
from
tutorial.quickstart
import
views
router
=
routers
.
DefaultRouter
()
router
.
register
(
r
"users"
,
views
.
UserViewSet
)
router
.
register
(
r
"groups"
,
views
.
GroupViewSet
)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns
=
[
path
(
""
,
include
(
router
.
urls
)),
path
(
"api-auth/"
,
include
(
"rest_framework.urls"
,
namespace
=
"rest_framework"
)),
]
Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.
Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.
Pagination allows you to control how many objects per page are returned. To enable it add the following lines to
tutorial/settings.py
REST_FRAMEWORK
=
{
"DEFAULT_PAGINATION_CLASS"
:
"rest_framework.pagination.PageNumberPagination"
,
"PAGE_SIZE"
:
10
,
}
Settings
ΒΆ
Add
'rest_framework'
to
INSTALLED_APPS
. The settings module will be in
tutorial/settings.py
INSTALLED_APPS = [
...
'rest_framework',
]
Okay, we're done.
Testing our API
ΒΆ
We're now ready to test the API we've built. Let's fire up the server from the command line.
python
manage.py
runserver
We can now access our API, both from the command-line, using tools like
curl
...
bash:
curl
-u
admin
-H
'Accept: application/json; indent=4'
http://127.0.0.1:8000/users/
Enter
host
password
for
user
'admin'
:
{
"count"
:
1
,
"next"
:
null,
"previous"
:
null,
"results"
:
[
{
"url"
:
"http://127.0.0.1:8000/users/1/"
,
"username"
:
"admin"
,
"email"
:
"admin@example.com"
,
"groups"
:
[]
}
]
}
Or using the
httpie
, command line tool...
bash:
http
-a
admin
http://127.0.0.1:8000/users/
http:
password
for
admin@127.0.0.1:8000::
$HTTP
/1.1
200
OK
...
{
"count"
:
1
,
"next"
:
null,
"previous"
:
null,
"results"
:
[
{
"email"
:
"admin@example.com"
,
"groups"
:
[]
,
"url"
:
"http://127.0.0.1:8000/users/1/"
,
"username"
:
"admin"
}
]
}
Or directly through the browser, by going to the URL
http://127.0.0.1:8000/users/
...
If you're working through the browser, make sure to login using the control in the top right corner.
Great, that was easy!
If you want to get a more in depth understanding of how REST framework fits together head on over to
the tutorial
, or start browsing the
API guide
. |
| Markdown | [Skip to content](https://www.django-rest-framework.org/tutorial/quickstart/#quickstart)
[](https://www.django-rest-framework.org/ "Django REST framework")
Django REST framework
Quickstart
Initializing search
[GitHub](https://github.com/encode/django-rest-framework "Go to repository")
- [Home](https://www.django-rest-framework.org/)
- [Tutorial](https://www.django-rest-framework.org/tutorial/quickstart/)
- [API Guide](https://www.django-rest-framework.org/api-guide/requests/)
- [Topics](https://www.django-rest-framework.org/topics/documenting-your-api/)
- [Community](https://www.django-rest-framework.org/community/tutorials-and-resources/)
[](https://www.django-rest-framework.org/ "Django REST framework") Django REST framework
[GitHub](https://github.com/encode/django-rest-framework "Go to repository")
- [Home](https://www.django-rest-framework.org/)
- Tutorial
Tutorial
- Quickstart
[Quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
Table of contents
- [Project setup](https://www.django-rest-framework.org/tutorial/quickstart/#project-setup)
- [Serializers](https://www.django-rest-framework.org/tutorial/quickstart/#serializers)
- [Views](https://www.django-rest-framework.org/tutorial/quickstart/#views)
- [URLs](https://www.django-rest-framework.org/tutorial/quickstart/#urls)
- [Pagination](https://www.django-rest-framework.org/tutorial/quickstart/#pagination)
- [Settings](https://www.django-rest-framework.org/tutorial/quickstart/#settings)
- [Testing our API](https://www.django-rest-framework.org/tutorial/quickstart/#testing-our-api)
- [1 - Serialization](https://www.django-rest-framework.org/tutorial/1-serialization/)
- [2 - Requests and responses](https://www.django-rest-framework.org/tutorial/2-requests-and-responses/)
- [3 - Class based views](https://www.django-rest-framework.org/tutorial/3-class-based-views/)
- [4 - Authentication and permissions](https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/)
- [5 - Relationships and hyperlinked APIs](https://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/)
- [6 - Viewsets and routers](https://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/)
- API Guide
API Guide
- [Requests](https://www.django-rest-framework.org/api-guide/requests/)
- [Responses](https://www.django-rest-framework.org/api-guide/responses/)
- [Views](https://www.django-rest-framework.org/api-guide/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/)
- [Parsers](https://www.django-rest-framework.org/api-guide/parsers/)
- [Renderers](https://www.django-rest-framework.org/api-guide/renderers/)
- [Serializers](https://www.django-rest-framework.org/api-guide/serializers/)
- [Serializer fields](https://www.django-rest-framework.org/api-guide/fields/)
- [Serializer relations](https://www.django-rest-framework.org/api-guide/relations/)
- [Validators](https://www.django-rest-framework.org/api-guide/validators/)
- [Authentication](https://www.django-rest-framework.org/api-guide/authentication/)
- [Permissions](https://www.django-rest-framework.org/api-guide/permissions/)
- [Caching](https://www.django-rest-framework.org/api-guide/caching/)
- [Throttling](https://www.django-rest-framework.org/api-guide/throttling/)
- [Filtering](https://www.django-rest-framework.org/api-guide/filtering/)
- [Pagination](https://www.django-rest-framework.org/api-guide/pagination/)
- [Versioning](https://www.django-rest-framework.org/api-guide/versioning/)
- [Content negotiation](https://www.django-rest-framework.org/api-guide/content-negotiation/)
- [Metadata](https://www.django-rest-framework.org/api-guide/metadata/)
- [Schemas](https://www.django-rest-framework.org/api-guide/schemas/)
- [Format suffixes](https://www.django-rest-framework.org/api-guide/format-suffixes/)
- [Returning URLs](https://www.django-rest-framework.org/api-guide/reverse/)
- [Exceptions](https://www.django-rest-framework.org/api-guide/exceptions/)
- [Status codes](https://www.django-rest-framework.org/api-guide/status-codes/)
- [Testing](https://www.django-rest-framework.org/api-guide/testing/)
- [Settings](https://www.django-rest-framework.org/api-guide/settings/)
- Topics
Topics
- [Documenting your API](https://www.django-rest-framework.org/topics/documenting-your-api/)
- [Internationalization](https://www.django-rest-framework.org/topics/internationalization/)
- [AJAX, CSRF & CORS](https://www.django-rest-framework.org/topics/ajax-csrf-cors/)
- [HTML & Forms](https://www.django-rest-framework.org/topics/html-and-forms/)
- [Browser Enhancements](https://www.django-rest-framework.org/topics/browser-enhancements/)
- [The Browsable API](https://www.django-rest-framework.org/topics/browsable-api/)
- [REST, Hypermedia & HATEOAS](https://www.django-rest-framework.org/topics/rest-hypermedia-hateoas/)
- Community
Community
- [Tutorials and Resources](https://www.django-rest-framework.org/community/tutorials-and-resources/)
- [Third Party Packages](https://www.django-rest-framework.org/community/third-party-packages/)
- [Contributing to REST framework](https://www.django-rest-framework.org/community/contributing/)
- [Project management](https://www.django-rest-framework.org/community/project-management/)
- [Release Notes](https://www.django-rest-framework.org/community/release-notes/)
- [3\.16 Announcement](https://www.django-rest-framework.org/community/3.16-announcement/)
- [3\.15 Announcement](https://www.django-rest-framework.org/community/3.15-announcement/)
- [3\.14 Announcement](https://www.django-rest-framework.org/community/3.14-announcement/)
- [3\.13 Announcement](https://www.django-rest-framework.org/community/3.13-announcement/)
- [3\.12 Announcement](https://www.django-rest-framework.org/community/3.12-announcement/)
- [3\.11 Announcement](https://www.django-rest-framework.org/community/3.11-announcement/)
- [3\.10 Announcement](https://www.django-rest-framework.org/community/3.10-announcement/)
- [3\.9 Announcement](https://www.django-rest-framework.org/community/3.9-announcement/)
- [3\.8 Announcement](https://www.django-rest-framework.org/community/3.8-announcement/)
- [3\.7 Announcement](https://www.django-rest-framework.org/community/3.7-announcement/)
- [3\.6 Announcement](https://www.django-rest-framework.org/community/3.6-announcement/)
- [3\.5 Announcement](https://www.django-rest-framework.org/community/3.5-announcement/)
- [3\.4 Announcement](https://www.django-rest-framework.org/community/3.4-announcement/)
- [3\.3 Announcement](https://www.django-rest-framework.org/community/3.3-announcement/)
- [3\.2 Announcement](https://www.django-rest-framework.org/community/3.2-announcement/)
- [3\.1 Announcement](https://www.django-rest-framework.org/community/3.1-announcement/)
- [3\.0 Announcement](https://www.django-rest-framework.org/community/3.0-announcement/)
- [Kickstarter Announcement](https://www.django-rest-framework.org/community/kickstarter-announcement/)
- [Mozilla Grant](https://www.django-rest-framework.org/community/mozilla-grant/)
- [Jobs](https://www.django-rest-framework.org/community/jobs/)
Table of contents
- [Project setup](https://www.django-rest-framework.org/tutorial/quickstart/#project-setup)
- [Serializers](https://www.django-rest-framework.org/tutorial/quickstart/#serializers)
- [Views](https://www.django-rest-framework.org/tutorial/quickstart/#views)
- [URLs](https://www.django-rest-framework.org/tutorial/quickstart/#urls)
- [Pagination](https://www.django-rest-framework.org/tutorial/quickstart/#pagination)
- [Settings](https://www.django-rest-framework.org/tutorial/quickstart/#settings)
- [Testing our API](https://www.django-rest-framework.org/tutorial/quickstart/#testing-our-api)
1. [Home](https://www.django-rest-framework.org/)
2. [Tutorial](https://www.django-rest-framework.org/tutorial/quickstart/)
# Quickstart[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#quickstart "Permanent link")
We're going to create a simple API to allow admin users to view and edit the users and groups in the system.
## Project setup[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#project-setup "Permanent link")
Create a new Django project named `tutorial`, then start a new app called `quickstart`.
Linux,
macOS
Windows
```
```
If you use Bash for Windows
```
```
The project layout should look like:
```
```
It may look unusual that the application has been created within the project directory. Using the project's namespace avoids name clashes with external modules (a topic that goes outside the scope of the quickstart).
Now sync your database for the first time:
```
python manage.py migrate
```
We'll also create an initial user named `admin` with a password. We'll authenticate as that user later in our example.
```
python manage.py createsuperuser --username admin --email admin@example.com
```
Once you've set up a database and the initial user is created and ready to go, open up the app's directory and we'll get coding...
## Serializers[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#serializers "Permanent link")
First up we're going to define some serializers. Let's create a new module named `tutorial/quickstart/serializers.py` that we'll use for our data representations.
```
```
Notice that we're using hyperlinked relations in this case with `HyperlinkedModelSerializer`. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
## Views[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#views "Permanent link")
Right, we'd better write some views then. Open `tutorial/quickstart/views.py` and get typing.
```
```
Rather than write multiple views we're grouping together all the common behavior into classes called `ViewSets`.
We can easily break these down into individual views if we need to, but using viewsets keeps the view logic nicely organized as well as being very concise.
## URLs[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#urls "Permanent link")
Okay, now let's wire up the API URLs. On to `tutorial/urls.py`...
```
```
Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.
Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.
## Pagination[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#pagination "Permanent link")
Pagination allows you to control how many objects per page are returned. To enable it add the following lines to `tutorial/settings.py`
```
```
## Settings[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#settings "Permanent link")
Add `'rest_framework'` to `INSTALLED_APPS`. The settings module will be in `tutorial/settings.py`
```
```
Okay, we're done.
***
## Testing our API[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#testing-our-api "Permanent link")
We're now ready to test the API we've built. Let's fire up the server from the command line.
```
python manage.py runserver
```
We can now access our API, both from the command-line, using tools like `curl`...
```
```
Or using the [httpie](https://httpie.io/docs#installation), command line tool...
```
```
Or directly through the browser, by going to the URL `http://127.0.0.1:8000/users/`...

If you're working through the browser, make sure to login using the control in the top right corner.
Great, that was easy\!
If you want to get a more in depth understanding of how REST framework fits together head on over to [the tutorial](https://www.django-rest-framework.org/tutorial/1-serialization/), or start browsing the [API guide](https://www.django-rest-framework.org/api-guide/requests/).
Back to top
Made with [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/) |
| Readable Markdown | We're going to create a simple API to allow admin users to view and edit the users and groups in the system.
## Project setup[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#project-setup "Permanent link")
Create a new Django project named `tutorial`, then start a new app called `quickstart`.
Linux, macOSWindows
```
```
If you use Bash for Windows
```
```
The project layout should look like:
```
```
It may look unusual that the application has been created within the project directory. Using the project's namespace avoids name clashes with external modules (a topic that goes outside the scope of the quickstart).
Now sync your database for the first time:
```
python manage.py migrate
```
We'll also create an initial user named `admin` with a password. We'll authenticate as that user later in our example.
```
python manage.py createsuperuser --username admin --email admin@example.com
```
Once you've set up a database and the initial user is created and ready to go, open up the app's directory and we'll get coding...
## Serializers[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#serializers "Permanent link")
First up we're going to define some serializers. Let's create a new module named `tutorial/quickstart/serializers.py` that we'll use for our data representations.
```
```
Notice that we're using hyperlinked relations in this case with `HyperlinkedModelSerializer`. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
## Views[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#views "Permanent link")
Right, we'd better write some views then. Open `tutorial/quickstart/views.py` and get typing.
```
```
Rather than write multiple views we're grouping together all the common behavior into classes called `ViewSets`.
We can easily break these down into individual views if we need to, but using viewsets keeps the view logic nicely organized as well as being very concise.
## URLs[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#urls "Permanent link")
Okay, now let's wire up the API URLs. On to `tutorial/urls.py`...
```
```
Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.
Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.
Pagination allows you to control how many objects per page are returned. To enable it add the following lines to `tutorial/settings.py`
```
```
## Settings[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#settings "Permanent link")
Add `'rest_framework'` to `INSTALLED_APPS`. The settings module will be in `tutorial/settings.py`
```
```
Okay, we're done.
***
## Testing our API[ΒΆ](https://www.django-rest-framework.org/tutorial/quickstart/#testing-our-api "Permanent link")
We're now ready to test the API we've built. Let's fire up the server from the command line.
```
python manage.py runserver
```
We can now access our API, both from the command-line, using tools like `curl`...
```
```
Or using the [httpie](https://httpie.io/docs#installation), command line tool...
```
```
Or directly through the browser, by going to the URL `http://127.0.0.1:8000/users/`...

If you're working through the browser, make sure to login using the control in the top right corner.
Great, that was easy\!
If you want to get a more in depth understanding of how REST framework fits together head on over to [the tutorial](https://www.django-rest-framework.org/tutorial/1-serialization/), or start browsing the [API guide](https://www.django-rest-framework.org/api-guide/requests/). |
| Shard | 170 (laksa) |
| Root Hash | 15945694212963949770 |
| Unparsed URL | org,django-rest-framework!www,/tutorial/quickstart/ s443 |