读django文档——Managing static files (e.g. images, JavaScript, CSS)

2023-05-16

在上一篇读django文档——nginx + uwsgi 部署django项目_苦行僧的妖孽日常-CSDN博客  部署django项目后,发现在runserver时都能正常部署的 static 文件都没有生效。查看文档解决该问题,记录这一过程。

If you use django.contrib.staticfiles as explained above, runserver will do this automatically when DEBUG is set to True. If you don’t have django.contrib.staticfiles in INSTALLED_APPS, you can still manually serve static files using the django.views.static.serve() view.

在INSTALLED_APPS里面配置django.contrib.staticfiles,并且DEBUG设置为True,那么runserver就可以自动获取static文件。这也是默认的配置。即使没有配置,也可以实现这个view来达到这个目的django.views.static.serve()。

这个默认的方式低效且不安全

Serving the site and your static files from the same server

当站点也作为静态文件服务器时,可以这样配置。

先在django项目目录里面创建一个目录作为STATIC_ROOT。

mkdir static

在django项目的settings.py里面加上STATIC_ROOT。

STATIC_ROOT = os.path.join(BASE_dir, 'static/')

然后执行 collectstatic,把静态文件收集到这个 STATIC_ROOT 目录里面。再验证静态文件就正常加载了。

python manage.py collectstatic

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

读django文档——Managing static files (e.g. images, JavaScript, CSS) 的相关文章

随机推荐