django - 什么时候应该使用media_root或static_root? [英] django - when should I use media_root or static_root?

查看:180
本文介绍了django - 什么时候应该使用media_root或static_root?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对django中的静态文件和媒体文件感到困惑。我在其他地方见过人们可以互换使用。



什么时候我应该使用 media_root ,何时应该使用 static_root



如果我有网站的图片我应该把它放在静态吗?如果我有产品图像,我把它放在媒体中?

解决方案

MEDIA_ROOT 是放置文件上传的目录,以及通常存储生成的文件的位置。例如,我的一个Django应用程序允许用户上传图像。在其中一个模型类中,我使用 sorl-thumbnail <>的 ImageField / a>与 upload_to ='%Y-%m'。每当用户上传图像时,该文件都存储在 MEDIA_ROOT /%Y-%m / (含%Y 替换为当前年份,而%m 替换为当前月份号)。此外,当sorl-thumbnail生成上传图像的缩略图时,它会将缩略图默认放置在$ code> MEDIA_ROOT / cache / 中的某个地方。



STATIC_ROOT 用于配置静态资产的放置目录。例如,网页设计中使用的网站样式表,JavaScript文件和图像是进入 STATIC_ROOT 的文件类型。如果您有多个安装的应用程序,每个使用静态文件的应用程序都可以有自己的静态文件目录。您使用 collectstatic 管理功能(通过 python manage.py collectstatic 调用)来复制所有应用程序的静态文件进入 STATIC_ROOT


I'm confused about static files and media files in django. I've seen elsewhere that people use it interchangeably.

When should I use media_root and when should I use static_root?

If I have site images should I put it in static? And if I have product images do I put it in media?

解决方案

MEDIA_ROOT is the directory where file uploads are placed, as well as where generated files are usually stored. For example, one of my Django apps allows users to upload images. In one of the model classes, I use the ImageField type from sorl-thumbnail with upload_to='%Y-%m'. Whenever a user uploads an image, the file is stored in MEDIA_ROOT/%Y-%m/ (with %Y replaced with the current year and %m replaced with the current month number). Also, when sorl-thumbnail generates a thumbnail for an uploaded image, it places the thumbnail by default somewhere in MEDIA_ROOT/cache/.

STATIC_ROOT is used to configure the directory where static assets are placed. For example, site stylesheets, JavaScript files, and images used in the design of web pages are the types of files that go into STATIC_ROOT. If you have multiple installed apps, each app that uses static files can have its own static files directory. You use the collectstatic management function (invoked via python manage.py collectstatic) to copy all of the apps' static files into STATIC_ROOT.

这篇关于django - 什么时候应该使用media_root或static_root?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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