2013-09-30 57 views
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? --> 

请注意,它只是抱怨非管理员网址。

最后,我也注意到,如果我跑./manage.py collectstatic,它仅收集管理文件到我的STATIC_ROOT目录,而不是我的应用程序的媒体文件。此外,即使我清除了STATIC_ROOT目录,管理链接仍然有效。

我如何通过这些404错误工作,让我所有的静态文件送达正确?

+0

向我们展示你的PROJECT_DIR定义 – lalo

+0

它'PROJECT_DIR = os.path.dirname(os.path.abspath则(__ __文件))',转化为我的应用程序的目录。 – mobopro

+0

它说在服务器输出任何东西/日志为什么找不到文件? –

回答

1

settings.py设置你的STATICFILES_DIRS变量包含的路径静态资产在哪里。这应该使他们与DEBUG = True

运行./manage.py runserver时,也我会用环境瓦尔延长元组STATICFILES_DIRS

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

你可以有你的环境变量设置为您的部署过程的一部分,并协助有灵活的部署流程。

对不起,切线位,并希望这有助于:)

相关问题