[django] Django 정적 파일 404

내 정적 파일을 가져올 수 없습니다. 다양한 설정과 디렉토리 구성 등을 시도했지만 404로 나타납니다. debug_toolbar가 설치되어 있으므로 STATIC_URL이 내 요청 컨텍스트에 도달하고 있음을 알 수 있습니다.

/ static을 표시하는 디렉토리 구조 (또한 시도해보기 위해 식사 앱 폴더 및 사용자 내부에 디렉토리를 배치했습니다.

/mealmate
    /mealmate
    /meals
    /static
        /css
             /bootstrap.min.css
    /templates
    /users

Settings.py (다른 다양한 설정을 실험했지만 몇 가지 중요한 설정) :

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

STATIC_URL = '/static/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

WSGI_APPLICATION = 'mealmate.wsgi.application'

base.html에서 렌더링

    <link rel="stylesheet" href="/static/css/bootstrap.min.css">

어떤 아이디어? 감사



답변

이것은 Windows 용 django에서 정적 / 미디어 / 템플릿 액세스를위한 작업 솔루션입니다.

settings.py

import os.path

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = ( os.path.join('static'), )


답변

나를 위해 이것은 디버그를 settings.py. 해결 방법은 --insecure스위치를 로 전달하는 runserver것이지만 실제 솔루션은 적절한 웹 서버를 사용하여 정적 파일을 제공하는 것입니다. 자세한 내용은 이 질문에 대한 답변 을 참조하십시오.


답변

웹 서버에서 실행중인 경우 정적 파일을 공용 액세스 가능 폴더에 복사하고 있습니까? 예를 들면 :

# web accessible folder
STATIC_ROOT = '/home/your_name/www/mealmate/static/'

# URL prefix for static files.
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # location of your application, should not be public web accessible 
    '/home/your_name/website/mealmate/static',
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

그런 다음이 게시물 Django 정적 파일을 사용하고 manage.py를 사용하여 정적 파일을 공용 액세스 가능 폴더에 복사 할 수 있습니다.

# --link    Create a symbolic link to each file instead of copying.
# --noinput Do NOT prompt the user for input of any kind.
#
python manage.py collectstatic -link --noinput

도움이 되었기를 바랍니다.


답변

위의 의견에서-이것을 실행하십시오

python manage.py findstatic --verbosity 2 css/styles.css

‘css / styles.css’에 일치하는 파일이 없습니다.

다음 위치에서 확인 :
/ Users / yourname / Documents / Workspace / test / staticfiles

정적 폴더의 이름 을 staticfiles 로 변경 했습니다. 모든 것이 잘되었습니다. (저는 osx + django 1.x에 있습니다)

비보안 모드를 사용하면 로컬 개발 상자에있는 경우 손상되지 않을 수 있습니다. 그렇지 않으면 여전히 404 오류가 발생할 수 있습니다.

python manage.py runserver --insecure

최신 정보

실제로 settings.py를 파헤쳐 서 침해 라인을 발견했습니다.

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'staticfiles'),
)


답변

나는 단순히

STATICFILES_DIRS = (
    '/absolute_path_to_project/mealmate/static',
)

이 작업을 수행합니다. 물론 absolute_path_to_project실제 경로로 바꾸고 필요한 경우 드라이브 문자를 추가하십시오.


답변

최근에 설정 파일에서 디버그를 false로 변경 한 경우. 이 절차를 따르십시오.

대부분의 경우 settings.py에서 디버그 설정이 false로 설정되어 발생합니다. –insecure 스위치를 runserver에 전달하면 작동합니다. 따라서 대신 python manage.py runserver 0.0.0.0:8000변경하면 python manage.py runserver 0.0.0.0:8000 --insecure.

작동합니다.


답변

Apache가 정적 디렉토리에 대한 요청을 차단했다는 사실을 깨달을 때까지 404 문제에 갇혀있었습니다. 내가 사용 python manage.py collectstatic받는 모든 정적 파일을 복사하는 정적의 내 프로젝트 루트, 즉 아래에 디렉토리를 / var에 / 내 / 사이트 / 정적 . 와

Alias /static /var/my/site/static
<Directory /var/my/site/static>
        Require all granted
</Directory>

에서 /etc/httpd/conf/httpd.conf 파일 , 지금은 제대로 작동합니다.

위의 답변 중 어느 것도 작동하지 않으면 서버 구성을 확인하는 것이 좋습니다.