๐Ÿ“’ [ TIL ] 2022.07.20_65์ผ์ฐจ # ์ตœ์ข… ํ”„๋กœ์ ํŠธ (7)

๋ฌธ๋ช…์ฃผยท2022๋…„ 7์›” 21์ผ
0
post-thumbnail

[ 2022-07-20 (์ˆ˜) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

drf ๋ฐฑ์—”๋“œ๊ฐœ๋ฐœ + ํ”„๋ก ํŠธ๊ฐœ๋ฐœ
: ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ๊ฒŒ์‹œ๋ฌผ ํฌ์ŠคํŒ… ๋ฐ ๊ฒŒ์‹œ๋ฌผ ๋””ํ…Œ์ผ ์‚ฌ์ง„ํŒŒ์ผ ์ถœ๋ ฅ ๊ตฌํ˜„

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

* ๊ฒŒ์‹œ๋ฌผ ํฌ์ŠคํŒ… ๋ฐ ๊ฒŒ์‹œ๋ฌผ ๋””ํ…Œ์ผ ์‚ฌ์ง„ํŒŒ์ผ ์ถœ๋ ฅ ๊ตฌํ˜„
ํ”„๋ก ํŠธ์™€ ๋ฐฑ์—”๋“œ ๋ ˆํฌ์ง€๊ฐ€ ๋”ฐ๋กœ ์žˆ์„๋•Œ, ํ”„๋ก ํŠธ์—์„œ ๋ฐฑ์—”๋“œ์— ์ €์žฅ๋œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•.

settings.py

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

media ํด๋”์— ์ €์žฅ๋  ์ˆ˜ ์žˆ๋„๋ก ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

article.urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    #article/
    path('', views.ArticleView.as_view()),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

๊ฒŒ์‹œ๋ฌผ ๊ด€๋ จ๋œ urls.py์— static์ดํ•˜ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

<๋ฉ”์ธ>.urls.py

#๋ ˆํฌ์ง€ ๋ฉ”์ธ urls.py์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/', include('user.urls')),
    path('article/', include('article.urls')),
] 
if settings.DEBUG: 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if ์ดํ•˜ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด์ œ ํ”„๋ก ํŠธ์—์„œ ๋ฐฑ์—”๋“œ ๋ ˆํฌ์ง€์˜ media์— ์žˆ๋Š” ํŒŒ์ผ๋“ค์„ ์ž์œ ๋กญ๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋‹ค์Œ์€ ํ”„๋ก ํŠธ

<img src="http://127.0.0.1:8000${image}" alt="" />

์ด์ œ ๋ฐฑ์—”๋“œ ๊ฒฝ๋กœ๋ฅผ ๋งํฌ๋ฅผ ๊ฑธ์–ด ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค.

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

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

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด