Django MEDIA_URL 和 MEDIA_ROOT
我正在尝试通过 Django 管理上传图像,然后在前端页面或仅通过 URL 查看该图像。
请注意,这一切都在我的本地计算机上。
我的设置如下:
MEDIA_ROOT = '/home/dan/mysite/media/'
MEDIA_URL = '/media/'
我已将 upload_to 参数设置为“images”,并且文件已正确上传到目录:
'/home/dan/mysite/media/images/myimage.png'
但是,当我尝试通过以下 URL 访问图像时:
http://127.0.0.1:8000/media/images/myimage.png
我收到 404 错误。
我需要为上传的媒体设置特定的 URLconf 模式吗?
任何建议表示赞赏。
谢谢。
I'm trying to upload an image via the Django admin and then view that image either in a page on the frontend or just via a URL.
Note this is all on my local machine.
My settings are as follows:
MEDIA_ROOT = '/home/dan/mysite/media/'
MEDIA_URL = '/media/'
I have set the upload_to parameter to 'images' and the file has been correctly uploaded to the directory:
'/home/dan/mysite/media/images/myimage.png'
However, when I try to access the image at the following URL:
http://127.0.0.1:8000/media/images/myimage.png
I get a 404 error.
Do I need to setup specific URLconf patters for uploaded media?
Any advice appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
更新 Django >= 1.7
根据 Django 2.1 文档:提供用户在开发过程中上传的文件
您不再需要
if settings.DEBUG
因为 Django 将处理确保这一点仅在调试模式下使用。Django <= 1.6 的原始答案
尝试将其放入您的 urls.py
有了这个,您可以在
DEBUG = True
(当您在本地计算机上运行时),但您可以在投入生产时让您的 Web 服务器配置提供静态媒体,并且DEBUG = False
UPDATE for Django >= 1.7
Per Django 2.1 documentation: Serving files uploaded by a user during development
You no longer need
if settings.DEBUG
as Django will handle ensuring this is only used in Debug mode.ORIGINAL answer for Django <= 1.6
Try putting this into your urls.py
With this you can serve the static media from Django when
DEBUG = True
(when you run on local computer) but you can let your web server configuration serve static media when you go to production andDEBUG = False
请仔细阅读 Django 官方 DOC,你会找到最合适的答案。
解决此问题的最佳且最简单的方法如下所示。
Please read the official Django DOC carefully and you will find the most fit answer.
The best and easist way to solve this is like below.
对于 Django 1.9,您需要根据文档添加以下代码:
有关更多信息,您可以参考此处:https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-files-uploaded-by-a-user -开发期间
For Django 1.9, you need to add the following code as per the documentation :
For more info, you can refer here : https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-files-uploaded-by-a-user-during-development
这是我在 Django 2.0 中所做的。首先在
setting.py
中设置 MEDIA_ROOT 和 MEDIA_URL在
TEMPLATE_CONTEXT_PROCESSORS
中启用media
context_processors
,然后通过添加您的
媒体上下文处理器已启用,现在每个
RequestContext
将包含一个变量MEDIA_URL
。现在您可以在
template_name.html
中访问它Here What i did in Django 2.0. Set First MEDIA_ROOT an MEDIA_URL in
setting.py
Then Enable the
media
context_processors
inTEMPLATE_CONTEXT_PROCESSORS
by addingYour
media context processor
is enabled, Now everyRequestContext
will contain a variableMEDIA_URL
.Now you can access this in your
template_name.html
是的。对于开发,只需将其添加到 URLconf 中即可轻松完成:
但是,对于生产,您需要使用 Apache、lighttpd、nginx 或您首选的 Web 服务器来提供媒体服务。
Yes. For development, it's as easy as adding this to your URLconf:
However, for production, you'll want to serve the media using Apache, lighttpd, nginx, or your preferred web server.
如果您使用的是 python 3.0+,请按如下所示配置您的项目
设置
主 URL
If you'r using python 3.0+ then configure your project as below
Setting
Main Urls
(至少)对于 Django 1.8:
使用
如果您如上所述 ,请确保在 urlpatterns = [] 中没有指向默认视图的“catch all”url 模式出现在该模式之前。由于 .append 会将添加的方案放在列表的末尾,因此当然只有在没有先前的 url 模式匹配的情况下才会对其进行测试。您可以通过使用类似这样的方法来避免这种情况,其中“catch all”url 模式添加在最后,独立于 if 语句:
, 'views.home', name='home')), , 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))如果您如上所述 ,请确保在 urlpatterns = [] 中没有指向默认视图的“catch all”url 模式出现在该模式之前。由于 .append 会将添加的方案放在列表的末尾,因此当然只有在没有先前的 url 模式匹配的情况下才会对其进行测试。您可以通过使用类似这样的方法来避免这种情况,其中“catch all”url 模式添加在最后,独立于 if 语句:
(at least) for Django 1.8:
If you use
as described above, make sure that no "catch all" url pattern, directing to a default view, comes before that in urlpatterns = []. As .append will put the added scheme to the end of the list, it will of course only be tested if no previous url pattern matches. You can avoid that by using something like this where the "catch all" url pattern is added at the very end, independent from the if statement:
, 'views.home', name='home')), , 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))as described above, make sure that no "catch all" url pattern, directing to a default view, comes before that in urlpatterns = []. As .append will put the added scheme to the end of the list, it will of course only be tested if no previous url pattern matches. You can avoid that by using something like this where the "catch all" url pattern is added at the very end, independent from the if statement:
以下是我使用 Django 1.10 为 django-publications 应用程序交付 PDF 时必须进行的更改.6:
在
settings.py
中使用与您相同的媒体目录定义:由 @thisisashwanipandey 提供,在项目的主
urls.py
中:以及修改@r-allela 在
settings.py
中提供的答案:Here are the changes I had to make to deliver PDFs for the django-publications app, using Django 1.10.6:
Used the same definitions for media directories as you, in
settings.py
:As provided by @thisisashwanipandey, in the project's main
urls.py
:and a modification of the answer provided by @r-allela, in
settings.py
:设置所有 URLconf 模式后您可能会遇到的另一个问题是变量
{{ MEDIA_URL }}
在您的模板中不起作用。要解决此问题,请确保在您的 settings.py 中添加TEMPLATE_CONTEXT_PROCESSORS
中的Another problem you are likely to face after setting up all your URLconf patterns is that the variable
{{ MEDIA_URL }}
won't work in your templates. To fix this,in your settings.py, make sure you addin your
TEMPLATE_CONTEXT_PROCESSORS
.按照上面提到的 =>3.0 调试模式的步骤
以及让我困惑的部分,上面的静态 URL 只在我的主项目 urls.py 文件中有效。 我第一次尝试添加到我的应用程序中,并想知道为什么我看不到图像。
最后确保您设置了以下内容:
Following the steps mentioned above for =>3.0 for Debug mode
And also the part that caught me out, the above static URL only worked in my main project urls.py file. I was first attempting to add to my app, and wondering why I couldn't see the images.
Lastly make sure you set the following:
对于 Django 1.10 来说是这样的:
This if for Django 1.10:
添加到 django 1.8 的 Micah Carrick 答案:
Adding to Micah Carrick answer for django 1.8:
这就是我在 Python 3.6 和 Django 1.11 中以 DEBUG = False 模式实现图像渲染所做的事情
This is what I did to achieve image rendering in DEBUG = False mode in Python 3.6 with Django 1.11
在生产环境中,Django 不会自动加载媒体根目录,因此我们可以通过在 URL 模式后添加以下代码来解决该问题:
如果您使用多个应用程序并且在主应用程序 url 上包含应用程序 url,只需添加以下代码主项目 URL 上的代码(配置)。
On production environment Django does not load the media root automatically so that we can overcome that issue by adding following codes right after URL patterns:
If you are using more than one app and if you are including app urls on main app url, just add this code(configuration) on main project URL.
将以下代码添加到“settings.py”以访问(打开或显示)上传的文件:
Add this code below to "settings.py" to access(open or display)uploaded files:
对于开发中的 Django 3.0+,您的主 urls.py 中应包含以下内容:
For Django 3.0+ in development have the below in your main urls.py:
在
settings.py
中添加此代码Add this code in
settings.py