본문 바로가기
Study/Python

[Django] Debug=False 후 MEDIA, STATIC 경로 설정

by Becoming a Hacker 2021. 9. 20.
반응형

settings.py

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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'main/static')

 

urls.py

from django.conf.urls import url
from django.views.static import serve

urlpatterns = [
    url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]

 

Template에서 사용 방법

{% load static %}
<img src="{% static 'my_app/example.jpg' %}" alt="My image">

 

서버 실행

python manage.py runserver 0.0.0.0:PORT

 

댓글