简体   繁体   English

为什么Django可以提供媒体文件但不能提供静态文件

[英]Why django can serve media files but can not serve static files

I've set up Apache for my django project, and here's the code that I add in the Apache httpd.conf file: 我已经为我的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>

The server runs without problem, but the strange thing is, when I browse my website, I find that all the media files are successfully loaded, but all the static files can not be loaded (In the browser console ,it reports a 404 (NOT FOUND) error). 服务器运行没有问题,但是奇怪的是,当我浏览网站时,我发现所有媒体文件都已成功加载,但是所有静态文件都无法加载(在浏览器控制台中,它报告404(否)。找到)错误)。

Here are the settings in the settings.py file about media files and static files: 以下是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"),)

And in the urls.py, I didn't add anything like urlpatterns +=static(...,...) 在urls.py中,我没有添加urlpatterns + = static(...,...)

Can anyone help me figure out why media files are okay but static files cannot be loaded? 谁能帮我弄清楚为什么媒体文件还可以,但无法加载静态文件?

Django doesn't handle static files in production. Django在生产中不处理静态文件。 You need to link the static files with apache or if using Nginx 您需要使用apache或使用Nginx链接静态文件

Mod_wsgi: Mod_wsgi:

Alias /static/ /path/to/project/static/
<Directory /path/to/project/static>
Require all granted
</Directory>

or nginx: 或nginx:

location /static/ {
        alias /opt/project/static/;
    }

At least this is the correct way to do it, let your server handle them, or in nginx's case your reverse proxy. 至少这是正确的方法,让服务器处理它们,或者在nginx的情况下,使用反向代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM