Skip to main content
 首页 » 编程设计

python之Django/Apache/Mod_WSGI之静态文件的 HTTP 404 错误

2025年05月04日50lonelyxmas

我正在尝试在 Windows 上使用 Django + Apache + WSGI。

我一直在使用 Bitnami 堆栈,以便它负责 Apache 的安装。我能够在 Django 上放置一个 Django 项目,但是,它加载网页时没有任何静态文件(css、js)。所以我打开了 Apache 日志,所有静态文件都显示为 404。

这是我的 httpd-app.conf:

<Directory "E:/Bitnami/djangostack-1.6.7-1/apps/django/django_projects/Dashboard_Web/Dashboard_Web"> 
    Options +MultiViews 
    AllowOverride All 
    <IfVersion < 2.3 > 
        Order allow,deny 
        Allow from all 
    </IfVersion> 
    <IfVersion >= 2.3> 
        Require all granted 
        Options All 
    </IfVersion> 
    Options +ExecCGI 
 
 
WSGIApplicationGroup %{GLOBAL} 
    <IfVersion < 2.3 > 
        Order allow,deny 
        Allow from all 
    </IfVersion> 
    <IfVersion >= 2.3> 
        Require all granted 
        Options All 
    </IfVersion> 
    Options All 
    AllowOverride All 
    Options Indexes FollowSymLinks 
    Options +ExecCGI            
</Directory> 
 
 
Alias /static/ 'E:/Bitnami/djangostack-1.6.7-1/apache2/static/' 
 
<Directory 'E:/Bitnami/djangostack-1.6.7-1/apache2/static'> 
    <IfVersion < 2.3 > 
        Order allow,deny 
        Allow from all 
    </IfVersion> 
    <IfVersion >= 2.3> 
        Require all granted 
        Options All 
    </IfVersion> 
        Require all granted 
    Options All 
    AllowOverride All 
    Options Indexes FollowSymLinks 
</Directory> 
 
<Directory /> 
    Require all granted 
</Directory> 
 
WSGIScriptAlias /Dashboard_Web 'E:/Bitnami/djangostack-1.6.7-1/apps/django/django_projects/Dashboard_Web/Dashboard_Web/wsgi.py' 

这是我的设置.py:

STATIC_URL = '/static/' 
STATIC_ROOT = "E:/Bitnami/djangostack-1.6.7-1/apache2/static/" 

对于静态文件,我使用了 python manage.py collectstatic 它会生成所有静态文件 "E:/Bitnami/djangostack-1.6.7-1/apache2/static/”,所以我可以用 Fabric 自动执行它(所以我不需要自己输入 python manage.py collectstatic 来更新静态文件) .

别名似乎指向正确的文件夹,因为它可以在 Windows 资源管理器中搜索。所以我不确定为什么会这样,任何人都可以指出正确的方向吗?

谢谢。

编辑:这是 html 页面中的内容

<script type="text/javascript" src="/static/WebApp/jquery-2.1.1.min.js"></script> 
<script type="text/javascript" src="/static/WebApp/login.js"></script> 
<script type="text/javascript" src="/static/WebApp/bootstrap-3.2.0-dist/js/bootstrap.min.js"></script> 
<script type="text/javascript" src="/static/WebApp/zxcvbn-master/zxcvbn.js"></script> 
<script type="text/javascript" src="/static/WebApp/jquery.pwstrength.bootstrap-1.2.2/dist/pwstrength-bootstrap-1.2.2.min.js"></script> 

这就是我链接到静态文件的方式

{% load staticfiles %} 
<script type="text/javascript" src="{% static 'WebApp/jquery-2.1.1.min.js' %}"></script> 
<script type="text/javascript" src="{% static 'WebApp/index.js' %}"></script> 
<script type="text/javascript" src="{% static 'WebApp/bootstrap-3.2.0-dist/js/bootstrap.min.js' %}"></script> 
<script type="text/javascript" src="{% static 'WebApp/flot/jquery.flot.js' %}"></script> 
<script type="text/javascript" src="{% static 'WebApp/DataTables-1.10.2/media/js/jquery.dataTables.min.js' %}"></script> 

请您参考如下方法:

在我的例子中,我只需要为静态文件创建别名,在你的例子中,我的配置将如下所示:

WSGIDaemonProcess Dashboard_Web python-path=E:/Bitnami/djangostack-1.6.7-1/apps/django/django_projects/Dashboard_Web/ 
WSGIScriptAlias /Dashboard_Web E:/Bitnami/djangostack-1.6.7-1/apps/django/django_projects/Dashboard_Web/Dashboard_Web/wsgi.py process-group=Dashboard_web 
 
Alias /static/ E:/Bitnami/djangostack-1.6.7-1/apache2/static/ 
 
<Directory E:/Bitnami/djangostack-1.6.7-1/apps/django/django_projects/Dashboard_Web/> 
 
Order allow,deny 
 
Allow from all 
 
</Directory> 

当您运行多个 Django 应用程序时,值得使用此 WSGIDaemonProcess。另外你的问题可能是由你的系统引起的,你可以尝试使用“\”而不是“/”,例如:

Alias /static/ E:\Bitnami\djangostack-1.6.7-1\apache2\static\