Agile育成ブログ
未来を変える喜びを
Django Rest Framework

Django RestFramework[Youtube]


Warning: count(): Parameter must be an array or an object that implements Countable in /home/xs638785/agile-software.site/public_html/wp-content/plugins/rich-table-of-content/functions.php on line 490

仮想環境構築

python3 -m venv myvenv

requirements.txt

Django~=3.1.4
djangorestframework~=3.11.0
djangorestframework-simplejwt==4.1.2
djoser
pillow
django-cors-headers

requirements.txt のインストール

pip3 install -r requirements.txt

プロジェクト作成

django-admin startproject youtube_api

アプリケーション作成

django-admin startapp app

Webサーバー起動

python3 manage.py runserver

settings

import os
from datetime import timedelta

INSTALLED_APPSに追記

    'rest_framework',
    'api.apps.ApiConfig',
    'corsheaders',
    'djoser',

MIDDLEWAREに追記

'corsheaders.middleware.CorsMiddleware',

MIDDLEWAREに追記

CORS_ORIGIN_WHITELIST = [
    "http://localhost:3000"
]

MIDDLEWAREに追記

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

MIDDLEWAREに追記

SIMPLE_JWT = {
    'AUTH_HEADER_TYPES': ('JWT',),
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=30),
}

MIDDLEWAREに追記

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

You cannot copy content of this page