Django에서 정적 STATIC_URL과 STATIC_ROOT의 차이점
나는 혼란스럽고 static root
사물을 명확히하고 싶습니다.
Django에서 정적 파일을 제공하려면 settings.py
및에 있어야합니다 urls.py
.
import os
PROJECT_DIR=os.path.dirname(__file__)
1. 정적 파일을 수집해야하는 디렉토리의 절대 경로
STATIC_ROOT= os.path.join(PROJECT_DIR,'static_media/')
2. 정적 파일의 URL 접두사
STATIC_URL = '/static/'
3. 정적 파일의 추가 위치
STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,'static/'),)
... 그리고 urls.py
다음 줄에서 :
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}
))
4. 우리는 또한 사용 python manage.py collectstatic
질문 :
누구나 나에게 워크 플로우를 설명해 줄 수 있습니까? 어떻게 이상적인 작업을 수행해야합니까? 현재 위의 코드 스 니펫을 지정된 위치에 복사 / 붙여 넣기하고 정적 디렉토리에 새 파일을 계속 만들고 작동합니다. 그러나 내에서는
settings.STATIC_ROOT
다른 디렉토리를 가리 켰습니다.누군가가 각 설정의 워크 플로를 설명 할 수 있다면 좋을 것입니다. 파일 수집 및 관리 방법과 따라야 할 모범 사례.
감사.
STATIC_ROOT
./manage.py collectstatic
배포 할 정적 파일을 수집 할 디렉토리의 절대 경로 입니다. 예:STATIC_ROOT="/var/www/example.com/static/"
이제 명령 ./manage.py collectstatic
은 모든 정적 파일 (즉, 앱의 정적 폴더, 모든 경로의 정적 파일)을 디렉토리로 복사합니다 /var/www/example.com/static/
. 이제 아파치 또는 nginx..etc에서만이 디렉토리를 제공하면됩니다.
STATIC_URL
URL
에서 정적 파일이있는STATIC_ROOT
디렉토리 (Apache 또는 nginx..etc가) 제공됩니다. 예 :/static/
또는http://static.example.com/
을 설정 STATIC_URL = 'http://static.example.com/'
하면 url에서 apache 또는 nginx로 STATIC_ROOT
폴더 (예 "/var/www/example.com/static/"
:)를 제공해야 합니다 ( 'http://static.example.com/'
정적 파일 '/var/www/example.com/static/jquery.js'
을 참조 할 수 있도록 'http://static.example.com/jquery.js'
)
이제 장고 템플릿에서 다음과 같이 참조 할 수 있습니다.
{% load static %}
<script src="{% static "jquery.js" %}"></script>
렌더링됩니다 :
<script src="http://static.example.com/jquery.js"></script>
STATICFILES_DIRS
: 템플릿에 사용되는 것과 같은 프로젝트의 정적 파일을 여기에 보관할 수 있습니다.
STATIC_ROOT
: 이것을 비워두면 manage.py collectstatic
시스템의 모든 정적 파일을 검색하여 여기로 옮깁니다. 정적 파일 서버는 위치에 관계없이이 폴더에 매핑되어야합니다. collectstatic을 실행 한 후 확인하면 django가 빌드 한 디렉토리 구조를 찾을 수 있습니다.
--------편집하다----------------
@DarkCygnus가 지적한 것처럼 STATIC_ROOT는 파일 시스템의 디렉토리를 가리켜 야하며 폴더는 Django로 채워지기 때문에 비어 있어야합니다.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles'))
or
STATIC_ROOT = '/opt/web/project/static_files
'
--------End Edit -----------------
STATIC_URL
: '/static/' is usually fine, it's just a prefix for static files.
All the answers above are helpful but none solved my issue. In my production file, my STATIC_URL was https://<URL>/static
and I used the same STATIC_URL in my dev settings.py file.
This causes a silent failure in django/conf/urls/static.py.
The test elif not settings.DEBUG or '://' in prefix:
picks up the '//' in the URL and does not add the static URL pattern, causing no static files to be found.
It would be thoughtful if Django spit out an error message stating you can't use a http(s)://
with DEBUG = True
I had to change STATIC_URL to be '/static/'
'programing tip' 카테고리의 다른 글
NameNotFoundException 웹뷰 (0) | 2020.08.06 |
---|---|
버전 관리 저장소를 어떻게 구성합니까? (0) | 2020.08.06 |
AngularJS 지시문에서 '대체'속성이 더 이상 사용되지 않는 이유는 무엇입니까? (0) | 2020.08.06 |
iter와 into_iter의 차이점은 무엇입니까? (0) | 2020.08.06 |
Angular 2 템플릿에서 let- *는 무엇입니까? (0) | 2020.08.06 |