简体   繁体   English

python django中MEDIA_ROOT(媒体文件)和STATIC_ROOT(静态文件)的真正区别以及如何正确使用它们

[英]The real difference between MEDIA_ROOT (media files) and STATIC_ROOT (static files) in python django and how to use them correctly

The real difference between MEDIA_ROOT and STATIC_ROOT in python django and how to use them correctly? python django中MEDIA_ROOT和STATIC_ROOT的真正区别以及如何正确使用它们?

I just was looking for the answer and i'm still confused about it, in the end of the day i got two different answers:我只是在寻找答案,但我仍然对此感到困惑,最终我得到了两个不同的答案:

    - First is that the MEDIA_ROOT is for storing images and mp3 files maybe and the STATIC_ROOT for the css, js... and so on. - 首先是 MEDIA_ROOT 可能用于存储图像和 mp3 文件,而 STATIC_ROOT 用于 css、js...等等。

    -Second answer is that they were only using MEDIA_ROOT in the past for static files, and it caused some errors so eventually we are only using STATIC_ROOT. -第二个答案是他们过去只对 static 文件使用 MEDIA_ROOT,这会导致一些错误,所以最终我们只使用 STATIC_ROOT。

is one of them right if not be direct and simple please so everybody can understand and by how to use them correctly i mean what kind of files to put in them exactly如果不直接和简单,是其中之一,所以每个人都可以理解以及如何正确使用它们我的意思是要准确放入什么样的文件

Understanding the real difference between MEDIA_ROOT and STATIC_ROOT can be confusing sometimes as both of them are related to serving files.理解MEDIA_ROOTSTATIC_ROOT之间的真正区别有时可能会令人困惑,因为它们都与服务文件有关。

To be clear about their differences, I could point out their uses and types of files they serve.为了清楚它们的区别,我可以指出它们的用途和它们所服务的文件类型。

  1. STATIC_ROOT, STATIC_URL and STATICFILES_DIRS are all used to serve the static files required for the website or application. STATIC_ROOT, STATIC_URL and STATICFILES_DIRS都用于提供网站或应用程序所需的 static 文件。 Whereas, MEDIA_URL and MEDIA_ROOT are used to serve the media files uploaded by a user.MEDIA_URL and MEDIA_ROOT用于提供用户上传的媒体文件。

As you can see that the main difference lies between media and static files.如您所见,主要区别在于媒体文件和 static 文件之间。 So, let's differentiate them.所以,让我们区分它们。

  1. Static files are files like CSS, JS, JQuery, scss, and other images(PNG, JPG, SVG, etc. )etc. Static files are files like CSS, JS, JQuery, scss, and other images(PNG, JPG, SVG, etc. )etc. which are used in development, creation and rendering of your website or application.用于开发、创建和呈现您的网站或应用程序。 Whereas, media files are those files that are uploaded by the user while using the website.而媒体文件是用户在使用网站时上传的那些文件。

So, if there is a JavaScript file named main.js which is used to give some functionalities like show popup on button click then it is a STATIC file.因此,如果有一个名为 main.js 的main.js文件用于提供一些功能,例如单击按钮时显示弹出窗口,那么它就是一个 STATIC 文件。 Similarly, images like website logo, or some static images displayed in the website that the user can't change by any action are also STATIC files.类似的,像网站标志这样的图像,或者网站中显示的一些用户无法通过任何操作更改的 static 图像也是 STATIC 文件。

Hence, files(as mentioned above) that are used during the development and rendering of the website are known as STATIC files and are served by STATIC_ROOT, STATIC_URL or STATICFILES_DIRS(during deployment) in Django.因此,在网站开发和渲染期间使用的文件(如上所述)称为 STATIC 文件,并由 Django 中的 STATIC_ROOT、STATIC_URL 或 STATICFILES_DIRS(部署期间)提供服务。

Now for the MEDIA files: any file that the user uploads, for example;现在对于 MEDIA 文件:例如,用户上传的任何文件; a video, or image or excel file, etc. during the normal usage of the website or application are called MEDIA files in Django.在网站或应用程序正常使用期间的视频、图像或 excel 文件等在 Django 中称为 MEDIA 文件。

MEDIA_ROOT and MEDIA_URL are used to point out the location of MEDIA files stored in your application. MEDIA_ROOT 和 MEDIA_URL 用于指出存储在应用程序中的 MEDIA 文件的位置。

Hope this makes you clear.希望这能让你清楚。

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

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