1027 TIL

looggiยท2022๋…„ 10์›” 27์ผ
1

์ŠคํŒŒ๋ฅดํƒ€ ๋‚ด๋ฐฐ์บ  AI-3

๋ชฉ๋ก ๋ณด๊ธฐ
49/130
post-thumbnail

๐ŸŒž ์•„์นจํ€ด์ฆˆ

๐Ÿ”Ž simplejwt๋กœ ์ธ์ฆํ•˜๊ธฐ (ํ† ํฐ)

  • ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ, ์‹คํ–‰, ํŒจํ‚ค์ง€ ์„ค์น˜, requirements.txt ํŒŒ์ผ ์ƒ์„ฑ, .gitignore ํŒŒ์ผ ์ƒ์„ฑ, ๊นƒ ์›๊ฒฉ๋ฆฌํฌ ๋งŒ๋“ค์–ด์„œ ์—ฐ๊ฒฐ, ์žฅ๊ณ  ํ”„๋กœ์ ํŠธ ์‹œ์ž‘
  • morningquiz/settings.py
    INSTALLED_APPS> rest_framework, rest_framework_simplejwt ๋“ฑ๋ก
  • DB๋กœ migration
    python manage.py migrate
    model์„ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๊ธฐ๋•Œ๋ฌธ์— makemigrations๋Š” ํ•„์š”์—†๋‹ค
  • urls.py
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
path('api/token/', TokenObtainPairView.as_view(), name='...'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='...'),
]

adminํŽ˜์ด์ง€๋Š” ์‚ฌ์šฉ์•ˆํ•˜๋‹ˆ๊นŒ ์ง€์šฐ๋Š” ๊ฒŒ ๋” ์ข‹์Œ(import๋ถ€๋ถ„๋„ ์‹น๊ฐ™์ด-์ง€๊ธˆ ์ง€์šด์ƒํƒœ์ž„)

  • admin๊ณ„์ • ๋งŒ๋“ค๊ธฐ
    python manage.py createsuperuser

  • postman์œผ๋กœ ๋กœ๊ทธ์ธ ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

๐Ÿ’ก ์•Œ๊ฒŒ๋œ ๊ฒƒ

  • pip freeze๋กœ requirements.txt๋ฅผ ๋งŒ๋“ค์–ด์„œ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค pipenve/poetry ์ด์šฉํ•˜๋Š” ๊ฒŒ ๋” ์ข‹๋‹ค
    โžก Pipfile ํŒŒ์ผ๊ณผ Pipfile.lock ํŒŒ์ผ ๋ฐ›์•„์„œ $ pipenv install ๋กœ requirements.txt ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ์—ญํ• ์„ ํ•จ
    โžก HOW TO : $ pip install pipenv (์ „์—ญ์— ์„ค์น˜) ํ›„ $ pipenv --python 3.7
  • views.py๋ฅผ ๋งŒ๋“œ๋ ค๋‹ค๋ณด๋‹ˆ serializers.py๋„ ๊ฐ™์ด ๋งŒ๋“ค์–ด๋ฒ„๋ ธ๋Š”๋ฐ urls.py์—tj ํŒจํ‚ค์ง€์—์„œ ์„ค์น˜ํ•œ views๋ฅผ ์ž„ํฌํŠธํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ตณ์ด ๋งŒ๋“ค ๊ฒŒ ์—†๋‹ค
from django.urls import path
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

urlpatterns = [
    path('api/token/', TokenObtainPairView.as_view(), name='tokenobtain'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='tokenrefresh'),
]

๐Ÿ‹ ์‚ฌ๋ฌผ์ธ์‹ ํ”„๋กœ์ ํŠธ ์ฝ”๋“œ๋ฆฌ๋ทฐ 3์ผ์ฐจ

๐Ÿ”Ž ๋Œ“๊ธ€ ์ข‹์•„์š” ๊ธฐ๋Šฅ

๐Ÿ’ก ์•Œ๊ฒŒ๋œ ๊ฒƒ

  • project/settings.py์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•:
    • STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    • STATICFILES_DIRS = [BASE_DIR / 'static',]
  • project/urls.py์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•:
# ์‚ฌ์šฉ์ž๊ฐ€ ์—…๋กœ๋“œํ•˜๋Š” ์ด๋ฏธ์ง€ํŒŒ์ผ
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# ์›นํŽ˜์ด์ง€ ๊ตฌ์„ฑ์— ํ•„์š”ํ•œ ์ด๋ฏธ์ง€ํŒŒ์ผ
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

๐ŸŽƒ ์žฅ๊ณ  ์‹ฌํ™” 4์ฃผ์ฐจ ~ 5๊ฐ•

๐Ÿญ ์Œ์Œ์Œ

  • ์Šคํƒœํ‹ฑ ๊ฒฝ๋กœ ์„ค์ • ํ›„ ์ด๋ฏธ์ง€ ์„œ๋น™์ด ์ž˜ ๋˜๋Š”์ง€ ํ™•์ธ

  • serializers.py-serializer ์„ค์ •์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ํ˜•ํƒœ ๋ณ€ํ™”

ArticleSerializer

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields ='__all__'

โžก fields ๊ฐ€ all์ผ ๋•Œ ๋ชจ๋“  ํ•„๋“œ ์ถœ๋ ฅํ•จ

vs

ArticleListSerializer

class ArticleListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields =('pk','title','image','updated_at','user')

โžก ์ง€์ •ํ•œ ํ•„๋“œ๋งŒ ์ถœ๋ ฅ๋จ


  • ์‹œ๋ฆฌ์–ผ๋ผ์ด์ € ๋ฉ”์†Œ๋“œํ•„๋“œ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ

โžก def get_ํ•„๋“œ(self, ๋ชจ๋ธ):
return ๋ชจ๋ธ.ํ•„๋“œ.์ถœ๋ ฅ๋˜๊ธธ ์›ํ•˜๋Š” ๋‹ค๋ฅธ ํ•„๋“œ


  • ํ•„์ˆ˜์ ์œผ๋กœ ์ž…๋ ฅํ•ด์•ผํ•˜๋Š” ํ•„๋“œ ์ž…๋ ฅ ์•ˆํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

postman์—์„œ postํ•  ๋•Œ ๊ผญ ์ž…๋ ฅ๊ฐ’์ด ํ•„์š”ํ•œ ํ•„๋“œ(user)๊ฐ€ ์žˆ์–ด์„œ ์ž…๋ ฅ๊ฐ’์ด ์ž˜ ์ „์†ก๋๋Š”์ง€ ํ™•์ธ์ด ์–ด๋ ค์›€

{
    "user": [
        "This field is required."
    ]
}

1) postman-environment์—์„œ access token ๋ฐœ๊ธ‰๋ฐ›์•„์„œ initial value, current value์— ๋„ฃ๊ณ  ์ €์žฅ -> {{token}}์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ (์ด๊ฑด ๊ทธ๋ƒฅ ํ™˜๊ฒฝ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž„)
2) ์œ ์ €ํ•„๋“œ๊ฐ€ required๊ฐ€ ์•„๋‹Œ ์ƒˆ๋กœ์šด ์‹œ๋ฆฌ์–ผ๋ผ์ด์ €๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ ์šฉ! ์œ„์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ํ•„๋“œ๋ฅผ ๊ณจ๋ผ์„œ ๋„ฃ์–ด์ฃผ๋ฉด ๋จ ์ดˆ๊ฐ„๋‹จ


  • ์ด๋ฏธ์ง€ํŒŒ์ผ ๋ณด๋‚ด๋ณด๊ธฐ

body - form-data ์—์„œ
์„ค์ •ํ•œ ํ•„๋“œ๊ฐ’์„ ๋ชจ๋‘ value์— ๋„ฃ์–ด์ฃผ๊ณ  image๋Š” type: file ์„ ํƒ ํ›„ ์ „์†กํ•˜๋ฉด ok
โžก db์—์„œ ํ™•์ธ๊ฐ€๋Šฅ/ mediaํด๋”์—์„œ๋„ ์ด๋ฏธ์ง€ ์ €์žฅ๋œ ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ
๋ชจ๋ธ ์ด๋ฏธ์ง€ํ•„๋“œ ์•ˆ์— upload_to='%y%m%d'ํ•˜๋ฉด ๊ฒฝ๋กœ ์•ˆ์— ๋‚ ์งœ ํด๋”๊ฐ€ ์ƒ๊ฒจ์„œ ๊ทธ ์•ˆ์— ์ €์žฅ๋จ


๐Ÿญ ์˜ค๋ฅ˜

  • ModuleNotFoundError: No module named 'artticles'
    โžก settings.py/ INSTALLED_APPS์— ์ด๋ฆ„ ์ž˜๋ชป์จ๋†“์Œ

  • token is invalid

refresh token์œผ๋กœ access token ์žฌ๋ฐœ๊ธ‰๊นŒ์ง€ ๋ฐ›์•˜๋Š”๋ฐ๋„?? ๊ณ„์† ์•ˆ๋œ๋‹ค... ๋„ˆ๋ฌด.. ํž˜๋“ค๋‹ค..

๊ป„๊ป„...... local์— variable์„ ์ถ”๊ฐ€ํ•ด์•ผ๋˜๋Š”๋ฐ ํ† ํฐ์ด๋ผ๋Š” ํ™˜๊ฒฝ์„ ์ƒˆ๋กœ ๋งŒ๋“ค๊ณ  ์‹ฌ์ง€์–ด ์‚ฌ์šฉ๋„ ์•ˆํ•ด์„œ ์ฒดํฌ๋„ ์•ˆ๋ผ์žˆ์Œ ใ…‹ใ…‹ใ…‹ใ…‹... ํž˜๋“œ๋Ÿฌ.. ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด {{ token }}์ƒ‰๊น”์ด ๋‹คํ™์ƒ‰์ด ๋˜๋Š”๋ฐ ๋ˆˆ์น˜ ์ฑ˜์–ด์•ผ์ง€..

profile
looooggi

0๊ฐœ์˜ ๋Œ“๊ธ€