【发布时间】:2014-12-14 09:10:13
【问题描述】:
我已经为我的 django 项目设置了 Apache,这是我在 Apache httpd.conf 文件中添加的代码:
WSGIScriptAlias / E:/Django/mysite/mysite/wsgi.py
WSGIPythonPath E:Django/mysite/
<Directory E:/Django/mysite/mysite/>
<Files wsgi.py>
Require all granted`
</Files>
</Directory>
服务器运行没有问题,但奇怪的是,当我浏览我的网站时,我发现所有媒体文件都加载成功,但所有静态文件都无法加载(在浏览器控制台中,它报告了一个404(未找到)错误)。
这里是settings.py文件中关于媒体文件和静态文件的设置:
STATIC_URL = '/static/'
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
STATIC_ROOT=os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "media"),)
并且在 urls.py 中,我没有添加类似 urlpatterns +=static(...,...) 的东西
谁能帮我弄清楚为什么媒体文件没问题但无法加载静态文件?
【问题讨论】:
-
这是在开发/生产过程中如何完成的:docs.djangoproject.com/en/dev/howto/static-files 和 docs.djangoproject.com/en/dev/howto/static-files/deployment。就个人而言,我会尝试添加 url 模式:
urlpatterns = [<PATTERNS>] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) -
对于 Apache/mod_wsgi 参见 docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/…