Django静态文件 - 404 [英] Django Static Files - 404

查看:193
本文介绍了Django静态文件 - 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个很多 问题 on 这个,但没有一个似乎有帮助我一点儿这是这种情况。



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

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

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ static /'

#静态文件的附加位置
STATICFILES_DIRS =(
#将字符串放在这里,像/ home / html / static或C:/ www / django / static
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,而不是相对路径
'C:/ work / wincrash / dev / dev / analysis / static',

运行collect static成功从C:/ work / wincrash / dev / dev / analysis / static中提取所有静态文件,并将它们放在/静态/文件夹在该网站的根目录。



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

 < link type =text / csshref ={{STATIC_URL}} media / css / custom-theme / jquery-ui-1.8.22.custom.css =Stylesheet/> 
< script type =text / javascriptsrc ={{STATIC_URL}} media / js / jquery / jquery-1.7.2.min.js>< / script>
< script type =text / javascriptsrc ={{STATIC_URL}} media / js / jquery / jquery-ui-1.8.22.custom.min.js>< / script>

<! - Base css sheet - >
< link rel =stylesheethref ={{STATIC_URL}} media / css / base.csstype =text / cssmedia =all/>

当呈现HTML时,最终如此:

 < link type =text / csshref =/ static / media / css / custom-theme / jquery-ui-1.8.22.custom.css的rel = 样式表 > 
< script type =text / javascriptsrc =/ static / media / js / jquery / jquery-1.7.2.min.js>< / script>
< script type =text / javascriptsrc =/ static / media / js / jquery / jquery-ui-1.8.22.custom.min.js>< / script>
<! - Base css sheet - >
< link rel =stylesheethref =/ static / media / css / base.csstype =text / cssmedia =all>

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



感谢所有提前为您的帮助。我知道这可能是一个重复的问题,但是我在这里发现的其他所有问题都没有太大的帮助。我已经在这一段时间,只想移过去。

解决方案

我想出来了。 p>

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

 <?xml version =1.0encoding =UTF-8?> 
< configuration>
< system.webServer>
<处理程序>
<! -
这将删除Helicon Zoo处理程序,并使IIS处理静态文件。
- >
< remove name =django.project#x64/>
< remove name =django.project#x86/>
< / handlers>
< /system.webServer>
< / configuration>


I know there has been a lot of questions on this, but none of them seemed to help me at all. Here's the scenario.

I have a website using the Django Web Framework on IIS on Windows I've been working on. In order to deploy static files, I've been using the collectstatic functionality of the framework to collect the static files. This aspect of it works fine. From my 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',
)

Running collect static successfully pulls all my static files from C:/work/wincrash/dev/dev/analysis/static and puts them in the /static/ folder in the root directory of the site.

My problem occurs when I'm trying to load the static files on the webpage. Here's a snippet from my base.html page that loads on every page. None of the following static files are loading, failing with the 404 error:

<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" />

Which ends up like this when the HTML is rendered:

<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">

So I guess what I'm asking is what is preventing these files from being loaded? Is it the Django Framework? Is it IIS? The IIS logs are showing the 404 not found for all the static files. Why is this? How does IIS know where to look, and how can I help point it in the right direction?

Thanks for all in advance for your help. I know this might seem like a duplicate question but all the other questions I found on here weren't much help. I've been at this for a while and just would like to move past it.

解决方案

I figured it out.

I guess you need a web.config file in your static folder in order for IIS to find the static folder from which to service the static files. When I put a web.config file with the following content in the static folder where I had all the static files, it worked immediately.

<?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>

这篇关于Django静态文件 - 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆