【问题标题】:Problems serving static files on development server only仅在开发服务器上提供静态文件的问题
【发布时间】:2013-10-06 22:16:36
【问题描述】:

在我的开发服务器上提供静态文件时遇到问题。我将其配置如下。这是在我的 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 错误并正确提供所有静态文件?

【问题讨论】:

  • 向我们展示您的 PROJECT_DIR 定义
  • 它是PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)),翻译为我的应用程序目录。
  • 它在服务器输出/日志中是否说明了为什么找不到文件?

标签: django django-templates django-admin


【解决方案1】:

settings.py 中设置STATICFILES_DIRS 变量以包含静态资产所在的路径。这应该在运行 ./manage.py runserverDEBUG = True 时将它们引入

我还会使用环境变量来扩展 STATICFILES_DIRS 的元组

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

您可以将环境变量设置为部署过程的一部分,并有助于实现灵活的部署过程。

抱歉有点切线,希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2020-02-21
    • 1970-01-01
    相关资源
    最近更新 更多