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'), )
For me this turned out to be caused by setting debug to false in settings.py
. A workaround is to pass the --insecure
switch to runserver
, but the real solution is to use a proper web server to serve the static files. See the answers to this question for more details.
If you are running this on a web server are you copying the static files to a public accessible folder? For example:
# 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',
)
Then you can use this post Django static Files and copy the static files to the public accessible folder using 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
Hope that helps!
I simply added the equivalent of
STATICFILES_DIRS = (
'/absolute_path_to_project/mealmate/static',
)
to get this working. Of course, replace absolute_path_to_project
with your actual path and, if necessary, add the drive letter.
I was also stuck in the 404 problem until I realized that Apache had blocked the requests to the static dir. I used python manage.py collectstatic
to copy all the static files to the static dir under my project root, i.e. /var/my/site/static. With
Alias /static /var/my/site/static
<Directory /var/my/site/static>
Require all granted
</Directory>
in /etc/httpd/conf/httpd.conf, it works properly now.
If none of the answers above works, you may consider checking your server config.
Make sure mealmate is in your INSTALLED_APPS
from comments above - run this
python manage.py findstatic --verbosity 2 css/styles.css
No matching file found for 'css/styles.css'.
Looking in the following locations:
/Users/yourname/Documents/Workspace/test/staticfiles
I simply renamed my static folder to staticfiles and all was well. (I'm on osx + django 1.x)
use insecure mode may not hurt if you're on local dev box - otherwise you may still get 404 errors.
python manage.py runserver --insecure
UPDATE
actually digging into settings.py found the infringing line.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
I'm assuming you're using Django1.3+ here.
First off, you need to define a few more settings:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
path.join(TOP_DIR, 'static'),
]
STATIC_ROOT = path.join(TOP_DIR, 'staticfiles')
STATIC_URL = '/static/'
This should help you find that directory.
Also, you should always access your static files using STATIC URL
:
<link rel="stylesheet" href="{{ STATIC_URL }}css/bootstrap.min.css">
in Django you have one more advantage for your templates and it's you can link statics like this and maybe this is your problem!
<script src="{% static 'myapp/bootstrap.min.js' %}"></script>
<link href="{% static 'myapp/bootstrap.css' %}" rel="stylesheet" type="text/css"/>
its better to use
{% static 'filename' %}
and its also easier to know more about, i refer to https://docs.djangoproject.com/en/1.11/intro/tutorial06/
I think you missed to put the app INSTALLED_APPS in your settings.py file, you should add "mealmate"
INSTALLED_APPS = (
'mealmate',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
After that the static and command files will be visible to django. I was having trouble with commands and this is the way I fixed it.
In my case, all I had to do was re-run the server:
python manage.py runserver
참고URL : https://stackoverflow.com/questions/12809416/django-static-files-404
'IT TIP' 카테고리의 다른 글
새로운 C # 5.0 'async'및 'await'키워드가 다중 코어를 사용합니까? (0) | 2020.12.02 |
---|---|
Ubuntu에 PyCrypto 설치-빌드시 치명적인 오류 (0) | 2020.12.02 |
원격 마스터 분기 삭제, 현재 분기로 인해 거부 됨 (0) | 2020.12.02 |
Laravel은 모든 요청을 HTTPS로 리디렉션 (0) | 2020.12.02 |
TempData keep () 대 peek () (0) | 2020.12.02 |