在django中,媒体_ROOT和media_url到底是什么?
我阅读了有关 media_root 和然后,我可以稍微了解他们,但不多。
Media_root :
- 绝对文件系统通往目录的路径,该路径将用户启用文件 。
Media_url :
- 从 Media_root ,用于 管理存储的文件 。如果设置为非空值,则必须以斜线结束。您将需要 在开发和生产环境中配置这些文件 。
我经常看到它们如下所示:
# "settings.py"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
那么,什么是 Media_root
和 media_url
的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我解释了“ Media_root” 然后“ Media_url” 。
< Media_root>
“ Media_root” 设置通往目录的绝对路径,该目录存储了上传文件和设置“ Media_root”永远不会影响媒体文件URL 。
例如,我们有一个django项目:
\媒体“在我的情况下,在我的情况下 “媒体_root” :
并将下面的代码设置为 “ urls.py” :
并设置模型如下所示:“图像” :
并将下面的代码设置为 admin.py” :
然后,上传文件“橙色.jpg” :
然后,“媒体”文件夹是在与“ db.sqlite3” 和和“ manage.py” 的同一级别上创建的在Django Project root Directory 和上载的文件“ Orange.jpg” 存储在“媒体”文件夹中,如下所示:
然后,上传更多文件:
“ ” “更改图像”文件的页面如下所示:
”如下所示:
请小心,如果您从“ urls.py”删除下面的代码:
然后,文件“ okhate.jpg” 未显示。相反,存在一个错误,如下所示:
接下来,您可以将上传的文件存储在更多的subdirectories 下,我解释了两种方法,并且建议使用第一种方法,因为它是因为它是灵活的,不建议第二种方法,因为它根本不灵活。
在下存储上载文件的第一种方法是
首先,set “ os.path.join(base_dir,'Media')” to “ Media_root” 如下所示:
和,添加“ upload_to ='images /水果'“ to ” models.imagefield()” ,如下所示:
然后,上传的文件存储在” c:\ users \ users \ kai \ kai \ django-project \ media中\ images \ fruits“在我的情况下,在我的情况下,如下所示:
第二种将上传文件存储在中的第二种方法是首先,Set '媒体/图像/水果' “ os.path.join()”的第二个论点如下所示:
set 没有参数 如下所示:“ models.imagefield()” :
然后,上传的文件存储在“ c:\ users \ kai \ django-project \媒体\媒体\ images \ images \ fruits \ fruits”中,在我的情况下, Strong>如下所示,但如前所述,建议使用第一种方式,因为它是灵活的,而第二种方法根本不灵活:
此外,如果我们不设置 ”。 Media_root“ 如下所示:
或设置为空字符串 to “ OS.Path.join()”的第二个参数如下所示:
或DON' t SET “ OS.Path.join()”的第二个论点如下所示:
set 没有参数 “ models.imagefield()” 如下所示:
然后,上传的文件存储在与“ db.sqlite3” 和“ manage.py” 的相同级别Django Project root Directory 如下所示:
此外,在上传文件后,如果我们更改“ Media_root” ,我们将无法显示上传的文件,而我们仍然可以显示上传已上传。即使我们更改“ models.imagefield()” 。
例如,我们设置“ os.path.join(base_dir,'Media')” to “ Media_root” :
和,SET “ upload_to ='images/images/水果'“ to ” models.imagefield()” ,如下所示:
然后,上传文件“橙色
。 ://i.sstatic.net/mvui3.png“ rel =“ noreferrer”>
“图像/水果/橙色。
然后,单击 “ noreferrer”>
i.sstatic.net/xalrl.png“ rel = 显示“橙色.jpg” 如下所示:
现在,我们从 os.path中更改“ Media_root” 。 join(base_dir,'媒体')“ to ” os.path.join(base_dir,'Hello/world'):
然后,再次单击 “图像/水果/orange.jpg“ on “更改图像”页面的页面如下所示:
然后, file'Orange.jpg“ 未显示。相反,存在一个错误,如下所示:
然后,正如我之前所说的,即使我们更改“ models.imagefield()” 上传文件后,我们仍然可以显示上传文件。因此,现在,我们从“ os.path.join(base_dir,'Hello/world')” “ os.path.join) (base_dir,'媒体')“ :
and,更改” models.imagefield(upload_to ='images/fruits')” to “ models.imagefield(upload_to ='hello hello /world')” :
然后,再次单击“图像/水果/橙色。 :
然后,显示“橙色.jpg” 如下所示:
< media_url>
接下来,我解释了“ Media_url” 。
“ Media_url” sets 的目录(中间) 主机部分和媒体文件的文件部分URL ,如下所示设置“ Media_url”永远不会影响到存储上传文件的绝对路径:
例如,我们将'/Media/'设置为' Media_url“ :
set 没有参数 to ” models.imagefield()” ,如下所示:
然后,上传 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” 和文件零件“橙色
。 。
因此,只需更改“ Media_url” 从'/Media/'更改为'/images/fruits/'',如下所示:
然后,单击“橙色
。 sstatic.net/9isam.png“ alt =”在此处输入图像描述>
然后,目录部分“媒体” 更改为 “ image/fruits” 如下所示:
此外,我们可以将URL的目录部分设置为“ Media_url”和“ models.imagefield()” 的组合。在这种情况下,我们只能更改在上传文件后设置的目录部分的一部分,而我们不能更改 目录部分的部分由“ models.imagefield()设置的目录部分”。 “ 上传文件后:
例如,我们将'/Media/'设置为“ Media_url” ,如下所示:
并添加“ upload_to ='”图像/水果'“ to ” models.imagefield()” 如下所示:
然后,上传文件“ Orange.jpg” :
然后,转到文件的“更改图像”页面然后单击“ images/fruits/fruits/grange.jpg” :
/I.SSTATIC.NET/pmf8x.png“ rel = ,文件的URL 显示如下所示:
然后,目录部分是:
现在,我们更改“ Media_url” 来自'/媒体/' to '/hello/hello/world/':
更改“ models.imagefield(upload_to ='images/fruits')” 到“ models.imagefield(upload_to ='hey/erach')” :
然后,单击 “ images/fruits/fruits/grange.jpg” 再次:
然后,文件的URL 显示如下所示:
然后,我们可以更改目录'媒体'的一部分'在上传后,“ media_url” 设置的世界' '橙色。 /arter'由“ models.imagefield()”设置上传 文件“橙色
。 Strong>如下所示:
或SET 一个空字符串 to “ Media_url” 如下所示:
或SET 一个或多个斜线 如下所示:“ Media_url” :
set 没有参数 to “ models.imagefield()” ,如下所示:
然后,没有目录零件< /strong>设置主机部件“ localhost:8000” 和文件零件“橙色
。 i.sstatic.net/2qfup.jpg“ rel =“ noreferrer”>
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:
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":
And set the code below to "urls.py":
And set the model "Image" as shown below:
And set the code below to "admin.py":
Then, upload the file "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:
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:
Then, the file "orange.jpg" is displayed as shown below:
Be careful, if you remove the code below from "urls.py":
Then, the file "orange.jpg" is not displayed. 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:
And, add "upload_to='images/fruits'" to "models.ImageField()" as shown below:
Then, uploaded files are stored in "C:\Users\kai\django-project\media\images\fruits" in Windows in my case as shown below:
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:
And set no arguments to "models.ImageField()" as shown below:
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:
In addition, if we don't set "MEDIA_ROOT" as shown below:
Or set an empty string to the second argument of "os.path.join()" as shown below:
Or don't set the second argument of "os.path.join()" as shown below:
And set no arguments to "models.ImageField()" as shown below:
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:
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":
And, set "upload_to='images/fruits'" to "models.ImageField()" as shown below:
Then, upload the file "orange.jpg":
Then, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:
Then, the file "orange.jpg" is displayed as shown below:
Now, we change "MEDIA_ROOT" from "os.path.join(BASE_DIR, 'media')" to "os.path.join(BASE_DIR, 'hello/world')":
Then again, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:
Then, the file "orange.jpg" is not displayed. 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. So now, we change back "MEDIA_ROOT" from "os.path.join(BASE_DIR, 'hello/world')" to "os.path.join(BASE_DIR, 'media')":
And, change "models.ImageField(upload_to='images/fruits')" to "models.ImageField(upload_to='hello/world')":
Then again, click on "images/fruits/orange.jpg" on "Change image" page of the file as shown below:
Then, the file "orange.jpg" is displayed as shown below:
<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:
For example, we set '/media/' to "MEDIA_URL":
And set no arguments to "models.ImageField()" as shown below:
Then, upload the file "orange.jpg":
Then, go to "Change image" page of the file then click on "orange.jpg":
Then, the URL of the file is displayed as shown below:
As you can see, the directory part "media" is set between the host part "localhost:8000" and the file part "orange.jpg"
And, this URL below is in this case of "www.example.com" with "https":
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:
Then, click on "orange.jpg" again:
Then, the directory part "media" is changed to "image/fruits" as shown below:
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:
And add "upload_to='images/fruits'" to "models.ImageField()" as shown below:
Then, upload the file "orange.jpg":
Then, go to "Change image" page of the file then click on "images/fruits/orange.jpg":
Then, the URL of the file is displayed as shown below:
Then, the directory part is:
Now, we change "MEDIA_URL" from '/media/' to '/hello/world/':
And, change "models.ImageField(upload_to='images/fruits')" to "models.ImageField(upload_to='hey/earth')":
Then, click on "images/fruits/orange.jpg" again:
Then, the URL of the file is displayed as shown below:
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":
In addition, if we don't set "MEDIA_URL" as shown below:
Or set an empty string to "MEDIA_URL" as shown below:
Or set one or more slashes to "MEDIA_URL" as shown below:
And set no arguments to "models.ImageField()" as shown below:
Then, no directory part is set between the host part "localhost:8000" and the file part "orange.jpg" as shown below: