2013-08-23 191 views
3

我知道出现了lotofquestionsonthis,但他们都不似乎帮助我。这是场景。Django的静态文件 - 404

我有一个网站在Windows上的IIS上使用Django Web框架我一直在努力。为了部署静态文件,我一直在使用框架的collectstatic功能来收集静态文件。它的这个方面工作正常。从我的settings.py:

STATIC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static')) 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
    'C:/work/wincrash/dev/dev/analysis/static', 
) 

运行收集静态成功地拉由C我所有的静态文件:/工作/ wincrash的/ dev的/ dev /分析/静,并将它们放在/静态/文件夹的根目录的网站。

我试图在网页上加载静态文件时出现问题。以下是我的base.html页面的一个片段,可以在每个页面上加载。下面的静态文件没有加载,与404错误而失败:

<link type="text/css" href="{{ STATIC_URL }}media/css/custom-theme/jquery-ui-1.8.22.custom.css" rel="Stylesheet" /> 
<script type="text/javascript" src="{{ STATIC_URL }}media/js/jquery/jquery-1.7.2.min.js"></script> 
<script type="text/javascript" src="{{ STATIC_URL }}media/js/jquery/jquery-ui-1.8.22.custom.min.js"></script> 

<!-- Base css sheet --> 
<link rel="stylesheet" href="{{ STATIC_URL }}media/css/base.css" type="text/css" media="all" /> 

其中,当HTML呈现这样结束:

<link type="text/css" href="/static/media/css/custom-theme/jquery-ui-1.8.22.custom.css" rel="Stylesheet"> 
<script type="text/javascript" src="/static/media/js/jquery/jquery-1.7.2.min.js"></script> 
<script type="text/javascript" src="/static/media/js/jquery/jquery-ui-1.8.22.custom.min.js"></script> 
<!-- Base css sheet --> 
<link rel="stylesheet" href="/static/media/css/base.css" type="text/css" media="all"> 

所以我猜我问的是什么正在阻止这些文件被加载?它是Django框架吗?它是IIS吗? IIS日志显示没有找到所有静态文件的404。为什么是这样? IIS如何知道在哪里看,以及如何帮助将它指向正确的方向?

非常感谢您的帮助。我知道这可能看起来像一个重复的问题,但我在这里找到的所有其他问题没有太大的帮助。我已经有一段时间了,只想走过去。

回答

2

我想通了。

我想你需要一个web.config文件在你的静态文件夹中,以便IIS找到静态文件的静态文件夹。当我将静态文件放在静态文件夹中的以下内容的web.config文件时,它立即生效。

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
    <handlers> 
     <!-- 
     This removes Helicon Zoo handler and makes IIS processing static files. 
     --> 
     <remove name="django.project#x64" /> 
     <remove name="django.project#x86" /> 
    </handlers> 
    </system.webServer> 
</configuration>