https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html#installation
새 프로젝트 만들기
python -m venv venv
source venv/Scripts/Activate
pip install django djangorestframework djangorestframework-simplejwt
pip freeze > requirementx.txt
django-admin startproject drf_project .
.gitignore
git init
git remote add origin < >
git add .
git commit -m " "
git push origin main
drf_project > settings.py:
INSTALLED_APPS = [
'rest_framework',
'rest_framework_simplejwt',
'users',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
}
drf_project > urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
]
users > urls.py:
from django.urls import path
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]
models.py / admin.py
https://docs.djangoproject.com/en/4.2/topics/auth/customizing/
Hashed password
users > serializer.py:
from rest_framework import serializers
from users.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
def create(self, validated_data):
user = super().create(validated_data)
password = user.password
user.set_password(password)
user.save()
return user
Customizing token
https://django-rest-framework-simplejwt.readthedocs.io/en/latest/customizing_token_claims.html
access 토큰
refresh 토큰으로 access 토큰 재발급
cors-headers
pip install django-cors-headers
pip freeze > requirements.txt
drf_project > settings.py:
INSTALLED_APPS = [
"corsheaders",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware", # 낮아도 얘보다는 위에
]
CORS_ALLOW_ALL_ORIGINS = True
https://pypi.org/project/django-cors-headers/
로컬스토리지
https://www.w3schools.com/jsref/prop_win_localstorage.asp
'Django' 카테고리의 다른 글
파이썬 장고 실무 심화 4주차_SerializerMethodField, StringRelatedField (0) | 2023.04.21 |
---|---|
Token 인증_JWT(JSON Web Token) (0) | 2023.04.21 |
파이썬 장고 실무 심화 2주차 4_drf-yasg, 클래스형 view(cbv) (3) | 2023.04.21 |
포스트맨(Postman) 사용법 (2) | 2023.04.19 |
파이썬 장고 실무 심화 2주차 3_PUT, DELETE (0) | 2023.04.19 |