This commit is contained in:
Bruno Alla 2025-12-15 19:27:27 +00:00 committed by GitHub
commit a272e6831c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 188 additions and 55 deletions

BIN
docs/img/logo-dark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
docs/img/logo-light.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

View File

@ -17,7 +17,7 @@
}
</style>
<p class="badges" height=20px>
<div class="badges">
<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>
<a href="https://github.com/encode/django-rest-framework/actions/workflows/main.yml">
@ -27,11 +27,10 @@
<a href="https://pypi.org/project/djangorestframework/">
<img src="https://img.shields.io/pypi/v/djangorestframework.svg" class="status-badge">
</a>
</p>
</div>
---
<p>
<h1 style="position: absolute;
width: 1px;
height: 1px;
@ -41,8 +40,8 @@
clip: rect(0,0,0,0);
border: 0;">Django REST Framework</h1>
<img alt="Django REST Framework" title="Logo by Jake 'Sid' Smith" src="img/logo.png" width="600px" style="display: block; margin: 0 auto 0 auto">
</p>
![Django REST Framework](img/logo-light.png#only-light)
![Django REST Framework](img/logo-dark.png#only-dark)
Django REST framework is a powerful and flexible toolkit for building Web APIs.
@ -79,27 +78,35 @@ The following packages are optional:
Install using `pip`, including any optional packages you want...
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
```bash
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
```
...or clone the project from github.
git clone https://github.com/encode/django-rest-framework
```bash
git clone https://github.com/encode/django-rest-framework
```
Add `'rest_framework'` to your `INSTALLED_APPS` setting.
INSTALLED_APPS = [
...
'rest_framework',
]
```python
INSTALLED_APPS = [
# ...
"rest_framework",
]
```
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.
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls'))
]
```python
urlpatterns = [
# ...
path("api-auth/", include("rest_framework.urls"))
]
```
Note that the URL path can be whatever you want.
@ -111,44 +118,51 @@ We'll create a read-write API for accessing information on the users of our proj
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:
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
```python
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly"
]
}
```
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS`.
We're ready to create our API now.
Here's our project's root `urls.py` module:
from django.urls import path, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
```python
from django.urls import path, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'is_staff']
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ["url", "username", "email", "is_staff"]
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
# 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'))
]
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r"users", UserViewSet)
# 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")),
]
```
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.

BIN
docs/theme/img/favicon.ico vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
docs/theme/img/grid.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
docs/theme/img/logo.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

81
docs/theme/stylesheets/extra.css vendored Normal file
View File

@ -0,0 +1,81 @@
:root > * {
/* primary */
--md-primary-fg-color: #2c2c2c;
--md-primary-fg-color--light: #a8a8a8;
--md-primary-fg-color--dark: #181818;
/* accent */
--md-accent-fg-color: #c50d0d;
--md-accent-fg-color--light: #ff8f8f;
--md-accent-fg-color--dark: #A30000;
/* Style links */
--md-typeset-a-color: var(--md-typeset-color);
}
/* Dark theme customisation */
[data-md-color-scheme="slate"]
{}
.md-header {
border-top: 5px solid var(--md-accent-fg-color--dark);
}
body hr {
border-top: 1px dotted var(--md-accent-fg-color--dark);
}
.badges {
display: flex;
flex-direction: row-reverse;
align-content: end;
gap: 8px;
}
/* Cutesy quote styling */
[dir="ltr"] .md-typeset blockquote {
font-family: Georgia, serif;
font-size: 18px;
font-style: italic;
margin: 0.25em 0;
padding: 0.25em 40px;
line-height: 1.45;
position: relative;
color: var(--md-typeset-color);
border-left: none;
}
[dir="ltr"] .md-typeset blockquote:before {
display: block;
content: "\201C";
font-size: 80px;
position: absolute;
left: -10px;
top: -20px;
color: #7a7a7a;
}
[dir="ltr"] .md-typeset blockquote p:last-child {
color: #999999;
font-size: 14px;
display: block;
margin-top: 5px;
}
.md-typeset a {
color: var(--md-accent-fg-color--dark);
}
/* Replacement for `body { background-attachment: fixed; }`, which
has performance issues when scrolling on large displays. */
body::before {
content: ' ';
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: #f8f8f8;
background: url(../img/grid.png) repeat-x;
will-change: transform;
z-index: -1;
}

View File

@ -5,13 +5,57 @@ site_description: Django REST framework - Web APIs for Django
repo_url: https://github.com/encode/django-rest-framework
theme:
name: mkdocs
custom_dir: docs_theme
name: material
favicon: theme/img/favicon.ico
logo: theme/img/logo.png
palette:
- media: "(prefers-color-scheme)"
primary: custom
accent: custom
toggle:
icon: material/brightness-auto
name: "Switch to light mode"
- media: "(prefers-color-scheme: light)"
scheme: default
primary: custom
accent: custom
toggle:
icon: material/brightness-7
name: "Switch to dark mode"
- media: "(prefers-color-scheme: dark)"
scheme: slate
primary: custom
accent: custom
toggle:
icon: material/brightness-4
name: "Switch to system preference"
features:
- content.tabs.link
- content.code.annotate
- content.code.copy
- navigation.tabs
- navigation.instant
- navigation.instant.prefetch
- navigation.instant.progress
- navigation.path
- navigation.sections
- navigation.top
- navigation.tracking
- search.suggest
- toc.follow
extra_css:
- theme/stylesheets/extra.css
markdown_extensions:
- admonition
- toc:
anchorlink: True
permalink: true
- pymdownx.highlight:
pygments_lang_class: true
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.superfences
nav:
- Home: 'index.md'
@ -86,9 +130,3 @@ nav:
- 'Kickstarter Announcement': 'community/kickstarter-announcement.md'
- 'Mozilla Grant': 'community/mozilla-grant.md'
- 'Jobs': 'community/jobs.md'
extra_css:
- css/copy-button.css
extra_javascript:
- js/copy-button.js

View File

@ -1,5 +1,5 @@
# MkDocs to build our documentation.
mkdocs==1.6.0
mkdocs-material[imaging]==9.7.0
# pylinkvalidator to check for broken links in documentation.
pylinkvalidator==0.3