简体   繁体   English

MEDIA_ROOT 与 MEDIA_URL(Django)

[英]MEDIA_ROOT vs MEDIA_URL (Django)

I read the documentation about MEDIA_ROOT and MEDIA_URL then I could understand them a little bit but not much.我阅读了有关MEDIA_ROOTMEDIA_URL的文档,然后我可以稍微了解它们,但了解不多。

MEDIA_ROOT :媒体根目录

MEDIA_URL :媒体网址

I frequently see them as shown below:我经常看到它们如下所示:

# "settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

So, what are "MEDIA_ROOT" and "MEDIA_URL" exactly?那么, “MEDIA_ROOT”“MEDIA_URL”到底是什么?

First of all, I explain about "MEDIA_ROOT" then "MEDIA_URL" .首先,我解释一下“MEDIA_ROOT” ,然后是“MEDIA_URL”

<MEDIA_ROOT> <MEDIA_ROOT>

"MEDIA_ROOT" sets the absolute path to the directory where uploaded files are stored and setting "MEDIA_ROOT" never ever influence to media file URL . “MEDIA_ROOT”设置上传文件存储目录的绝对路径,设置“MEDIA_ROOT”永远不会影响媒体文件 URL

For example, we have a django project:比如我们有一个django的项目:

在此处输入图像描述

Then, we set "os.path.join(BASE_DIR, 'media')" which is "C:\Users\kai\django-project\media" in Windows in my case to "MEDIA_ROOT" :然后,我们将“os.path.join(BASE_DIR, 'media')”设置为“MEDIA_ROOT”

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Here
MEDIA_URL = '/media/'

And set the code below to "urls.py" :并将下面的代码设置为“urls.py”

# "core/urls.py"

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And set the model "Image" as shown below:并设置model“Image”如下图:

# "myapp/models.py"

class Image(models.Model):
    image = models.ImageField()

    def __str__(self):
        return str(self.image)

And set the code below to "admin.py" :并将下面的代码设置为“admin.py”

# "myapp/admin.py"

from .models import Image

admin.site.register(Image)

Then, upload the file "orange.jpg" :然后,上传文件“orange.jpg”

在此处输入图像描述

Then, "media" folder is created at the same level as "db.sqlite3" and "manage.py" which is just under the django project root directory and the uploaded file "orange.jpg" is stored in "media" folder as shown below:然后,在 django 项目根目录下创建与“db.sqlite3”“manage.py”相同级别的“media”文件夹,上传的文件“orange.jpg”存储在“media”文件夹中如下所示:

在此处输入图像描述

Then, upload more files:然后,上传更多文件:

在此处输入图像描述

In addition, we can display the file "orange.jpg" by clicking on "orange.jpg" on "Change image" page of the file as shown below:另外,我们可以在文件的“更改图片”页面点击 orange.jpg”来显示文件“orange.jpg” ,如下图:

在此处输入图像描述

Then, the file "orange.jpg" is displayed as shown below:然后,文件“orange.jpg”显示如下:

在此处输入图像描述

Be careful, if you remove the code below from "urls.py" :请注意,如果您从“urls.py”中删除以下代码:

# "core/urls.py"

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Then, the file "orange.jpg" is not displayed.然后,不显示文件“orange.jpg” Instead, there is an error as shown below:相反,出现如下所示的错误:

在此处输入图像描述

Next, you can store uploaded files under more subdirectories and I explain 2 ways to do that and the first way is recommended because it is flexible and the second way is not recommended because it is not flexible at all.接下来,您可以将上传的文件存储在更多的子目录下,我解释了两种方法,推荐第一种方式,因为它灵活,不推荐第二种方式,因为它根本不灵活。

The first way to store uploaded files under more subdirectories is first, set "os.path.join(BASE_DIR, 'media')" to "MEDIA_ROOT" as shown below:将上传的文件存放在更多子目录下的第一种方法是首先将“os.path.join(BASE_DIR, 'media')”设置为“MEDIA_ROOT” ,如下所示:

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Here
MEDIA_URL = '/media/'

And, add "upload_to='images/fruits'" to "models.ImageField()" as shown below:并且,将“upload_to='images/fruits'”添加到“models.ImageField()” ,如下所示:

# "myapp/models.py"

from django.db import models  

class Image(models.Model):    # Here
    image = models.ImageField(upload_to='images/fruits')

    def __str__(self):
        return str(self.image)

Then, uploaded files are stored in "C:\Users\kai\django-project\media\images\fruits" in Windows in my case as shown below:然后,上传的文件存储在“C:\Users\kai\django-project\media\images\fruits”中,在我的例子中是Windows,如下图:

在此处输入图像描述

The second way to store uploaded files under more subdirectories is first, set 'media/images/fruits' to the second argument of "os.path.join()" as shown below:第二种将上传的文件存储在更多子目录下的方法是首先将'media/images/fruits'设置为“os.path.join()”的第二个参数,如下所示:

# "core/settings.py"
                                    # Here
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/images/fruits')
MEDIA_URL = '/media/'

And set no arguments to "models.ImageField()" as shown below:并将no arguments设置为“models.ImageField()” ,如下所示:

# "myapp/models.py"

from django.db import models  

class Image(models.Model): 
    image = models.ImageField() # Here

    def __str__(self):
        return str(self.image)

Then, uploaded files are stored in "C:\Users\kai\django-project\media\images\fruits" in Windows in my case as shown below but as I said before, the first way is recommended because it is flexible while the second way is not flexible at all:然后,上传的文件存储在“C:\Users\kai\django-project\media\images\fruits”中,在我的例子中是Windows,如下所示,但正如我之前所说,推荐第一种方式,因为它灵活,而第二种方式根本不灵活:

在此处输入图像描述

In addition, if we don't set "MEDIA_ROOT" as shown below:另外,如果我们不设置“MEDIA_ROOT” ,如下图:

# "core/settings.py"

# MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Here
MEDIA_URL = '/media/'

Or set an empty string to the second argument of "os.path.join()" as shown below:或者将空字符串设置为“os.path.join()”的第二个参数,如下所示:

# "core/settings.py"
                                  
MEDIA_ROOT = os.path.join(BASE_DIR, '') # Here
MEDIA_URL = '/media/'

Or don't set the second argument of "os.path.join()" as shown below:或者不设置“os.path.join()”的第二个参数,如下所示:

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR) # Here
MEDIA_URL = '/media/'

And set no arguments to "models.ImageField()" as shown below:并将no arguments设置为“models.ImageField()” ,如下所示:

# "myapp/models.py"

from django.db import models  

class Image(models.Model): 
    image = models.ImageField() # Here

    def __str__(self):
        return str(self.image)

Then, uploaded files are stored at the same level as "db.sqlite3" and "manage.py" which is just under the django project root directory as shown below:然后,上传的文件存储在与“db.sqlite3”“manage.py”相同的级别,就在 django 项目根目录下,如下所示:

在此处输入图像描述

In addition, after uploading files if we change "MEDIA_ROOT" , we cannot display uploaded files while we can still display uploaded files even if we change "models.ImageField()" .另外,上传文件后,如果我们改变“MEDIA_ROOT” ,我们不能显示上传的文件,而即使我们改变“models.ImageField()” ,我们仍然可以显示上传的文件。

For example, we set "os.path.join(BASE_DIR, 'media')" to "MEDIA_ROOT" :例如,我们将“os.path.join(BASE_DIR, 'media')”设置为“MEDIA_ROOT”

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Here
MEDIA_URL = '/media/'

And, set "upload_to='images/fruits'" to "models.ImageField()" as shown below:并且,将“upload_to='images/fruits'”设置为“models.ImageField()” ,如下所示:

# "myapp/models.py"

from django.db import models  

class Image(models.Model):    # Here
    image = models.ImageField(upload_to='images/fruits')

    def __str__(self):
        return str(self.image)

Then, upload the file "orange.jpg" :然后,上传文件“orange.jpg”

在此处输入图像描述

Then, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:然后,在文件的“更改图像”页面上单击“图像/水果/orange.jpg” ,如下所示:

在此处输入图像描述

Then, the file "orange.jpg" is displayed as shown below:然后,文件“orange.jpg”显示如下:

在此处输入图像描述

Now, we change "MEDIA_ROOT" from "os.path.join(BASE_DIR, 'media')" to "os.path.join(BASE_DIR, 'hello/world')" :现在,我们将“MEDIA_ROOT”“os.path.join(BASE_DIR, 'media')”更改为“os.path.join(BASE_DIR, 'hello/world')”

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'hello/world') # Here
MEDIA_URL = '/media/'

Then again, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:然后再次点击文件“更改图像”页面上的“images/fruits/orange.jpg” ,如下图所示:

在此处输入图像描述

Then, the file "orange.jpg" is not displayed.然后,不显示文件“orange.jpg” Instead, there is an error as shown below:相反,出现如下所示的错误:

在此处输入图像描述

Then, as I said before, even if we change "models.ImageField()" after uploading files, we can still display uploaded files.然后,正如我之前所说,即使我们在上传文件后更改“models.ImageField()” ,我们仍然可以显示上传的文件。 So now, we change back "MEDIA_ROOT" from "os.path.join(BASE_DIR, 'hello/world')" to "os.path.join(BASE_DIR, 'media')" :所以现在,我们将“MEDIA_ROOT”“os.path.join(BASE_DIR, 'hello/world')”改回“os.path.join(BASE_DIR, 'media')”

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Here
MEDIA_URL = '/media/'

And, change "models.ImageField(upload_to='images/fruits')" to "models.ImageField(upload_to='hello/world')" :并且,将“models.ImageField(upload_to='images/fruits')”更改为“models.ImageField(upload_to='hello/world')”

# "myapp/models.py"

from django.db import models  

class Image(models.Model):    # Here
    image = models.ImageField(upload_to='hello/world')

    def __str__(self):
        return str(self.image)

Then again, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:然后再次点击文件“更改图像”页面上的“images/fruits/orange.jpg” ,如下图所示:

在此处输入图像描述

Then, the file "orange.jpg" is displayed as shown below:然后,文件“orange.jpg”显示如下:

在此处输入图像描述

<MEDIA_URL> <媒体网址>

Next, I explain about "MEDIA_URL" .接下来,我解释一下“MEDIA_URL”

"MEDIA_URL" sets the directory(middle) part of media file URL between the host part and the file part of media file URL as shown below and setting "MEDIA_URL" never ever influence to the absolute path to the directory where uploaded files are stored : “MEDIA_URL”在主机部分和媒体文件 URL 的文件部分之间设置媒体文件 URL 的目录(中间)部分,如下所示,设置“MEDIA_URL”永远不会影响上传文件存储目录的绝对路径

             Host        Directory      File
               |             |           |
        <-------------> <----------> <-------->      
https://www.example.com/media/images/orange.jpg

For example, we set '/media/' to "MEDIA_URL" :例如,我们将'/media/'设置为"MEDIA_URL"

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/' # Here

And set no arguments to "models.ImageField()" as shown below:并将no arguments设置为“models.ImageField()” ,如下所示:

# "myapp/models.py"

from django.db import models  

class Image(models.Model): 
    image = models.ImageField() # Here

    def __str__(self):
        return str(self.image)

Then, upload the file "orange.jpg" :然后,上传文件“orange.jpg”

在此处输入图像描述

Then, go to "Change image" page of the file then click on "orange.jpg" :然后,go 到文件的“更改图像”页面,然后单击“orange.jpg”

在此处输入图像描述

Then, the URL of the file is displayed as shown below:然后,文件的URL显示如下:

在此处输入图像描述

As you can see, the directory part "media" is set between the host part "localhost:8000" and the file part "orange.jpg"如您所见,目录部分“media”设置在主机部分“localhost:8000”文件部分“orange.jpg”之间

            Host    Directly   File
              |         |       |
       <------------> <---> <-------->      
http://localhost:8000/media/orange.jpg

And, this URL below is in this case of "www.example.com" with "https" :并且,下面的这个 URL 在这种情况下是带有“https”“www.example.com ”:

             Host     Directly   File
               |          |       |
        <-------------> <---> <-------->      
https://www.example.com/media/orange.jpg

And, we can change the directory part of URL even after uploading files.而且,我们甚至可以在上传文件后更改URL 的目录部分

So, just change "MEDIA_URL" from '/media/' to '/images/fruits/' as shown below:因此,只需将“MEDIA_URL”“/media/”更改为“/images/fruits/” ,如下所示:

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/images/fruits/' # Here

Then, click on "orange.jpg" again:然后,再次点击“orange.jpg”

在此处输入图像描述

Then, the directory part "media" is changed to "image/fruits" as shown below:然后,目录部分“media”更改为“image/fruits” ,如下所示:

在此处输入图像描述

In addition, we can set the directory part of URL with the combination of "MEDIA_URL" and "models.ImageField()" .另外,我们可以通过组合 "MEDIA_URL" 和 "models.ImageField()" 来设置 URL 的目录部分 In this case, we can only change the part of the directory part set by "MEDIA_URL" after uploading files while we cannot change the part of the directory part set by "models.ImageField()" after uploading files:在这种情况下,我们只能在上传文件后更改“MEDIA_URL”设置的目录部分,而不能在上传文件后更改“models.ImageField ()”设置的目录部分

For example, we set '/media/' to "MEDIA_URL" as shown below:例如,我们将'/media/'设置为“MEDIA_URL” ,如下所示:

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/' # Here

And add "upload_to='images/fruits'" to "models.ImageField()" as shown below:并在“models.ImageField ()”中添加“upload_to='images/fruits'” ,如下图:

# "myapp/models.py"

from django.db import models  

class Image(models.Model):    # Here
    image = models.ImageField(upload_to='images/fruits')

    def __str__(self):
        return str(self.image)

Then, upload the file "orange.jpg" :然后,上传文件“orange.jpg”

在此处输入图像描述

Then, go to "Change image" page of the file then click on "images/fruits/orange.jpg" :然后,go 到文件的“更改图像”页面,然后单击“图像/水果/orange.jpg”

在此处输入图像描述

Then, the URL of the file is displayed as shown below:然后,文件的URL显示如下:

在此处输入图像描述

Then, the directory part is:然后,目录部分是:

media/images/fruits

Now, we change "MEDIA_URL" from '/media/' to '/hello/world/' :现在,我们将“MEDIA_URL”“/media/”更改为“/hello/world/”

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/hello/world/' # Here

And, change "models.ImageField(upload_to='images/fruits')" to "models.ImageField(upload_to='hey/earth')" :并且,将“models.ImageField(upload_to='images/fruits')”更改为“models.ImageField(upload_to='hey/earth')”

# "myapp/models.py"

from django.db import models  

class Image(models.Model):              # Here
    image = models.ImageField(upload_to='hey/earth')

    def __str__(self):
        return str(self.image)

Then, click on "images/fruits/orange.jpg" again:然后,再次点击“images/fruits/orange.jpg”

在此处输入图像描述

Then, the URL of the file is displayed as shown below:然后,文件的URL显示如下:

在此处输入图像描述

Then, we could change the part of the directory part 'media' to 'hello/world' set by "MEDIA_URL" after uploading the file "orange.jpg" while we couldn't change the part of the directory part 'images/fruits' to 'hey/earth' set by "models.ImageField()" after uploading the file "orange.jpg" :然后,我们可以在上传文件“orange.jpg ”后将目录部分“media”的部分更改为“MEDIA_URL”设置的“hello/world” ,而我们无法更改目录部分“images/fruits” '上传文件“orange.jpg”由“models.ImageField()”设置的'hey/earth'

hello/world/images/fruits

In addition, if we don't set "MEDIA_URL" as shown below:另外,如果我们不设置“MEDIA_URL” ,如下图:

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# MEDIA_URL = '/media/' # Here

Or set an empty string to "MEDIA_URL" as shown below:或者将空字符串设置为“MEDIA_URL” ,如下所示:

# "core/settings.py"
                                  
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '' # Here

Or set one or more slashes to "MEDIA_URL" as shown below:或者给“MEDIA_URL”设置一个或多个斜杠,如下图:

# "core/settings.py"

MEDIA_ROOT = os.path.join(BASE_DIR)
MEDIA_URL = '/////' # Here

And set no arguments to "models.ImageField()" as shown below:并将no arguments设置为“models.ImageField()” ,如下所示:

# "myapp/models.py"

from django.db import models  

class Image(models.Model): 
    image = models.ImageField() # Here

    def __str__(self):
        return str(self.image)

Then, no directory part is set between the host part "localhost:8000" and the file part "orange.jpg" as shown below:然后,主机部分“localhost:8000”文件部分“orange.jpg”之间没有设置目录部分,如下所示:

在此处输入图像描述

http://localhost:8000/orange.jpg

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

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