在django中,媒体_ROOT和media_url到底是什么?

发布于 2025-01-25 14:58:52 字数 1486 浏览 3 评论 0 原文

我阅读了有关 media_root 然后,我可以稍微了解他们,但不多。

Media_root

Media_url

我经常看到它们如下所示:

# "settings.py"

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

那么,什么是 Media_root media_url 的?

I read the documentation about MEDIA_ROOT and MEDIA_URL then I could understand them a little bit but not much.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2025-02-01 14:58:52

首先,我解释了“ Media_root” 然后“ Media_url”

< Media_root>

“ Media_root” 设置通往目录的绝对路径,该目录存储了上传文件设置“ Media_root”永远不会影响媒体文件URL

例如,我们有一个django项目:

​\媒体“在我的情况下,在我的情况下 “媒体_root”

# "core/settings.py"

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

并将下面的代码设置为 “ urls.py”

# "core/urls.py"

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

并设置模型如下所示:“图像”

# "myapp/models.py"

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

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

并将下面的代码设置为 admin.py”

# "myapp/admin.py"

from .models import Image

admin.site.register(Image)

然后,上传文件“橙色.jpg”

然后,“媒体”文件夹是在与“ db.sqlite3” “ manage.py” 的同一级别上创建的在Django Project root Directory 上载的文件“ Orange.jpg” 存储在“媒体”文件夹中,如下所示:

然后,上传更多文件:

​“ ” “更改图像”文件的页面如下所示:

”如下所示:

“在此处输入图像描述”

请小心,如果您从“ urls.py”删除下面的代码

# "core/urls.py"

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

然后,文件“ okhate.jpg” 未显示。相反,存在一个错误,如下所示:

”在此处输入图像描述

接下来,您可以将上传的文件存储在更多的subdirectories 下,我解释了两种方法,并且建议使用第一种方法,因为它是因为它是灵活的,不建议第二种方法,因为它根本不灵活。

下存储上载文件的第一种方法
首先,set “ os.path.join(base_dir,'Media')” to “ Media_root” 如下所示:

# "core/settings.py"

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

和,添加“ upload_to ='images /水果'“ to ” 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)

然后,上传的文件存储在” c:\ users \ users \ kai \ kai \ django-project \ media中\ images \ fruits“在我的情况下,在我的情况下,如下所示:

”在此处输入图像说明“

第二种将上传文件存储在中的第二种方法是首先,Set '媒体/图像/水果' “ os.path.join()”的第二个论点如下所示:

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

set 没有参数 如下所示:“ 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)

然后,上传的文件存储在“ c:\ users \ kai \ django-project \媒体\媒体\ images \ images \ fruits \ fruits”中,在我的情况下, Strong>如下所示,但如前所述,建议使用第一种方式,因为它是灵活的,而第二种方法根本不灵活:

“在此处输入图像描述”

此外,如果我们不设置 ”。 Media_root“ 如下所示:

# "core/settings.py"

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

或设置为空字符串 to “ OS.Path.join()”的第二个参数如下所示:

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

或DON' t SET “ OS.Path.join()”的第二个论点如下所示:

# "core/settings.py"

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

set 没有参数 “ 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)

然后,上传的文件存储在与“ db.sqlite3” “ manage.py” 的相同级别Django Project root Directory 如下所示:

”“在此处输入图像说明”

此外,在上传文件后,如果我们更改“ Media_root” ,我们将无法显示上传的文件,而我们仍然可以显示上传已上传。即使我们更改“ models.imagefield()”

例如,我们设置“ os.path.join(base_dir,'Media')” to “ Media_root”

# "core/settings.py"

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

和,SET “ upload_to ='images/images/水果'“ to ” 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)

然后,上传文件“橙色

。 ://i.sstatic.net/mvui3.png“ rel =“ noreferrer”>

“图像/水果/橙色。

然后,单击 “ noreferrer”>

i.sstatic.net/xalrl.png“ rel = 显示“橙色.jpg” 如下所示:

https://i.sstatic.net/8wjfa.jpg“ alt =“在此处输入图像说明”>

现在,我们从 os.path中更改“ Media_root” 。 join(base_dir,'媒体')“ to ” os.path.join(base_dir,'Hello/world')

# "core/settings.py"

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

然后,再次单击 “图像/水果/orange.jpg“ on “更改图像”页面的页面如下所示:

“在此处输入映像”

然后, file'Orange.jpg“ 未显示。相反,存在一个错误,如下所示:

”在此处输入图像描述”

然后,正如我之前所说的,即使我们更改“ models.imagefield()” 上传文件后,我们仍然可以显示上传文件。因此,现在,我们从“ os.path.join(base_dir,'Hello/world')” “ os.path.join) (base_dir,'媒体')“

# "core/settings.py"

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

and,更改” models.imagefield(upload_to ='images/fruits')” to “ models.imagefield(upload_to ='hello 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)

然后,再次单击“图像/水果/橙色。 :

然后,显示“橙色.jpg” 如下所示:

“在此处输入映像”

< media_url>

接下来,我解释了“ Media_url”

“ Media_url” sets 的目录(中间) 主机部分和媒体文件的文件部分URL ,如下所示设置“ Media_url”永远不会影响到存储上传文件的绝对路径

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

例如,我们将'/Media/'设置为' Media_url“

# "core/settings.py"

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

set 没有参数 to ” 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)

然后,上传 file'grange'grange.jpg“

“在此处输入图像描述”

然后,转到“更改图像”文件然后单击 “橙色

。 =“ https://i.sstatic.net/wjr5p.png” rel =“ noreferrer”>

然后,文件的URL 显示如下所示:

< img src =“ https://i.sstatic.net/sedcx.jpg” alt =“在此处输入图像说明”>

如您所见,目录部分“媒体” is在之间设置主机部分“ localhost:8000” 文件零件“橙色

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

。 。

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

因此,只需更改“ Media_url” '/Media/'更改为'/images/fruits/'',如下所示:

# "core/settings.py"

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

然后,单击“橙色

。 sstatic.net/9isam.png“ alt =”在此处输入图像描述>

然后,目录部分“媒体” 更改为 “ image/fruits” 如下所示:

此外,我们可以将URL的目录部分设置为“ Media_url”和“ models.imagefield()” 的组合。在这种情况下,我们只能更改在上传文件后设置的目录部分的一部分,而我们不能更改 目录部分的部分由“ models.imagefield()设置的目录部分”。 “ 上传文件后:

例如,我们将'/Media/'设置为“ Media_url” ,如下所示:

# "core/settings.py"

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

并添加“ upload_to ='”图像/水果'“ to ” 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)

然后,上传文件“ Orange.jpg”

然后,转到文件的“更改图像”页面然后单击“ images/fruits/fruits/grange.jpg”

/I.SSTATIC.NET/pmf8x.png“ rel = ,文件的URL 显示如下所示:

“在此处输入图像说明”

然后,目录部分是:

media/images/fruits

现在,我们更改“ Media_url” 来自'/媒体/' to '/hello/hello/world/'

# "core/settings.py"

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

更改“ models.imagefield(upload_to ='images/fruits')” “ models.imagefield(upload_to ='hey/erach')”

# "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)

然后,单击 “ images/fruits/fruits/grange.jpg” 再次:

然后,文件的URL 显示如下所示:

“在此处输入图像说明”

然后,我们可以更改目录'媒体'的一部分'在上传后,“ media_url” 设置的世界' '橙色。 /arter'由“ models.imagefield()”设置上传 文件“橙色

hello/world/images/fruits

。 Strong>如下所示:

# "core/settings.py"

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

或SET 一个空字符串 to “ Media_url” 如下所示:

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

或SET 一个或多个斜线 如下所示:“ Media_url”

# "core/settings.py"

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

set 没有参数 to “ 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)

然后,没有目录零件< /strong>设置主机部件“ localhost:8000” 文件零件“橙色

。 i.sstatic.net/2qfup.jpg“ rel =“ noreferrer”>

http://localhost:8000/orange.jpg

First of all, I explain about "MEDIA_ROOT" then "MEDIA_URL".

<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.

For example, we have a django project:

enter image description here

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":

# "core/settings.py"

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

And set the code below to "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:

# "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":

# "myapp/admin.py"

from .models import Image

admin.site.register(Image)

Then, upload the file "orange.jpg":

enter image description here

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:

enter image description here

Then, upload more files:

enter image description here

In addition, we can display the file "orange.jpg" by clicking on "orange.jpg" on "Change image" page of the file as shown below:

enter image description here

Then, the file "orange.jpg" is displayed as shown below:

enter image description here

Be careful, if you remove the code below from "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. Instead, there is an error as shown below:

enter image description here

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:

# "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:

# "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:

enter image description here

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:

# "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:

# "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:

enter image description here

In addition, if we don't set "MEDIA_ROOT" as shown below:

# "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:

# "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:

# "core/settings.py"

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

And set no arguments to "models.ImageField()" as shown below:

# "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:

enter image description here

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()".

For example, we set "os.path.join(BASE_DIR, 'media')" to "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:

# "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":

enter image description here

Then, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:

enter image description here

Then, the file "orange.jpg" is displayed as shown below:

enter image description here

Now, we change "MEDIA_ROOT" from "os.path.join(BASE_DIR, 'media')" to "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:

enter image description here

Then, the file "orange.jpg" is not displayed. Instead, there is an error as shown below:

enter image description here

Then, as I said before, even if we change "models.ImageField()" after uploading files, we can still display uploaded files. So now, we change back "MEDIA_ROOT" from "os.path.join(BASE_DIR, 'hello/world')" to "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')":

# "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:

enter image description here

Then, the file "orange.jpg" is displayed as shown below:

enter image description here

<MEDIA_URL>

Next, I explain about "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:

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

For example, we set '/media/' to "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:

# "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":

enter image description here

Then, go to "Change image" page of the file then click on "orange.jpg":

enter image description here

Then, the URL of the file is displayed as shown below:

enter image description here

As you can see, the directory part "media" is set between the host part "localhost:8000" and the file part "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":

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

And, we can change the directory part of URL even after uploading files.

So, just change "MEDIA_URL" from '/media/' to '/images/fruits/' as shown below:

# "core/settings.py"

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

Then, click on "orange.jpg" again:

enter image description here

Then, the directory part "media" is changed to "image/fruits" as shown below:

enter image description here

In addition, we can set the directory part of URL with the combination of "MEDIA_URL" and "models.ImageField()". 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:

For example, we set '/media/' to "MEDIA_URL" as shown below:

# "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:

# "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":

enter image description here

Then, go to "Change image" page of the file then click on "images/fruits/orange.jpg":

enter image description here

Then, the URL of the file is displayed as shown below:

enter image description here

Then, the directory part is:

media/images/fruits

Now, we change "MEDIA_URL" from '/media/' to '/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')":

# "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:

enter image description here

Then, the URL of the file is displayed as shown below:

enter image description here

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":

hello/world/images/fruits

In addition, if we don't set "MEDIA_URL" as shown below:

# "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:

# "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:

# "core/settings.py"

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

And set no arguments to "models.ImageField()" as shown below:

# "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:

enter image description here

http://localhost:8000/orange.jpg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文