1

我无法在我的开发服务器上提供静态文件。我将其配置如下。这是在我的 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')

我对此感到好奇:

  1. 在生产服务器上一切正常。
  2. 在开发服务器上,我自己的文件出现 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 错误并正确提供所有静态文件?

4

1 回答 1

1

Set your STATICFILES_DIRS variable in settings.py to contain the path to where the static assets are. This should bring them in when running ./manage.py runserver with DEBUG = True

Also I would use environment vars to extend the tuple for STATICFILES_DIRS

if os.environ['deploy_type'] == 'prod':
    STATICFILES_DIRS += ("/var/www/html/static/",)
else:
    STATICFILES_DIRS += (os.path.join(PROJECT_DIR, 'static'),)

You can have your environment variables set as part of your deployment process and assist in having a flexible deployment process.

Sorry for the bit of a tangent and hope this helps :)

于 2014-02-05T04:10:46.943 回答