我无法在我的开发服务器上提供静态文件。我将其配置如下。这是在我的 settings.py 文件中:
STATIC_URL = '/static/'
if socket.gethostname() == 'production_server':
STATIC_ROOT = "/var/www/html/static/"
else:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
我对此感到好奇:
- 在生产服务器上一切正常。
在开发服务器上,我自己的文件出现 404 错误,但管理文件却没有......
http://localhost:8000/static/media/default.cssFailed to load resource: the server responded with a status of 404 (NOT FOUND) http://localhost:8000/static/media/javascript/pipe.jsFailed to load resource: the server responded with a status of 404 (NOT FOUND) http://localhost:8000/static/media/javascript/imdb_form.jsFailed to load resource: the server responded with a status of 404 (NOT FOUND) http://localhost:8000/static/media/pipe-img/wb-mpi-logo.pngFailed to load resource: the server responded with a status of 404 (NOT FOUND) http://localhost:8000/static/media/pipe-img/wb-mpi-logo-large.pngFailed to load resource: the server responded with a status of 404 (NOT FOUND)
...从这个模板...
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}media/default.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css" />
<link rel="shortcut icon" href="{{ STATIC_URL }}media/pipe-img/favicon.ico" type="image/x-icon" />
<link rel="icon" href="{{ STATIC_URL }}media/pipe-img/favicon.ico" type="image/x-icon">
<script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/core.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.init.js"></script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script type="text/javascript" src="{{ STATIC_URL }}media/javascript/pipe.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}media/javascript/imdb_form.js"></script> <!-- FUTURE: call from IMDB widget? -->
请注意,它只是抱怨非管理员 URL。
最后,我还注意到,如果我运行 ./manage.py collectstatic,它只会将管理文件收集到我的 STATIC_ROOT 目录中,而不是我的应用程序的媒体文件。此外,即使我清除了 STATIC_ROOT 目录,管理链接仍然有效。
如何解决这些 404 错误并正确提供所有静态文件?