python 在 Django 开发期间提供静态媒体:为什么不 MEDIA_ROOT?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2237418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:08:45  来源:igfitidea点击:

Serving static media during Django development: Why not MEDIA_ROOT?

pythondjangostatic-media

提问by Ram Rachum

I read this guideabout serving static media with Django during development.

我阅读了有关在开发过程中使用 Django 提供静态媒体的指南

I noticed that MEDIA_URLand MEDIA_ROOTwere not used in this. Why? What's the difference?

我注意到了这一点MEDIA_URLMEDIA_ROOT并没有在这方面使用。为什么?有什么不同?

I tried doing it with MEDIA_URLand MEDIA_ROOT, and got weird results.

我试着用MEDIA_URLand做它MEDIA_ROOT,得到了奇怪的结果。

回答by Frozenskys

In a production situation you will want your media to be served from your front end web server (Apache, Nginx or the like) to avoid extra load on the Django/Python process. The MEDIA_URL and MEDIA_ROOT are usually used for this.

在生产环境中,您希望从前端 Web 服务器(Apache、Nginx 等)提供媒体服务,以避免对 Django/Python 进程造成额外负载。MEDIA_URL 和 MEDIA_ROOT 通常用于此目的。

Running the built in Development server you will need to set the correct url in your url.py file - I normally use something like this:

运行内置的开发服务器,您需要在 url.py 文件中设置正确的 url - 我通常使用以下内容:

from django.conf import settings

urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)

Which picks up the MEDIA_ROOT from your settings file meaning that it works for development and live.

从您的设置文件中获取 MEDIA_ROOT 意味着它适用于开发和现场。

回答by jathanism

Straight from the comments in settings.py...

直接来自 settings.py 中的评论...

MEDIA_ROOT

MEDIA_ROOT

The MEDIA_ROOTis the absolute path to the directory that holds media such as /home/media/media.lawrence.com/.

MEDIA_ROOT是到保存介质,如目录的绝对路径/home/media/media.lawrence.com/

MEDIA_URL

媒体网址

The MEDIA_URLis the URL that handles the media served from MEDIA_ROOT. Make sure to use a trailing slash if there is a path component (optional in other cases). Examples: "http://media.lawrence.com", "http://example.com/media/".

MEDIA_URL是处理从媒体服务的URL MEDIA_ROOT。如果有路径组件(在其他情况下可选),请确保使用尾部斜杠。示例:“ http://media.lawrence.com”、“ http://example.com/media/”。

So, to reword those... The MEDIA_ROOTis where the files live physically on your system, and the MEDIA_URLis where those files are mapped to. In development, this might not always be accessible, and in most cases your dev environment and your production environment are not the same, and it is something you're going to have to go back and change. The other thing is that it is NOT A GOOD PRACTICE when Django was designed NOT to serve static content for you.

因此,要改写这些...这MEDIA_ROOT是文件在系统上的物理位置,MEDIA_URL也是这些文件映射到的位置。在开发中,这可能并不总是可访问的,并且在大多数情况下,您的开发环境和生产环境并不相同,您将不得不返回并进行更改。另一件事是,当 Django 被设计为不为您提供静态内容时,这不是一个好习惯。

If you're going to use this in development, I suggest you use the method of limiting it to DEBUG=True. Telling Django to serve static content from a temporary location while in development when the DEBUGis set to Trueis a much better and safer practice. You're NOT going to put your site into production with DEBUGon, right? Well, at least you shouldn't.

如果您打算在开发中使用它,我建议您使用将其限制为 DEBUG=True 的方法。在将DEBUG设置为时,告诉 Django 从临时位置提供静态内容True是一种更好、更安全的做法。你不会把你的网站投入生产,DEBUG对吧?好吧,至少你不应该。

Here is how I implemented it:

这是我如何实施它:

settings.py:

设置.py:

STATIC_DOC_ROOT = os.path.join(os.getcwd(), 'site_media')

urls.py:

网址.py:

from django.conf import settings
## debug stuff to serve static media
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', 
            {'document_root': settings.STATIC_DOC_ROOT}),
   )

This way any project I'm working on has a site_mediadirectory inside of it with all of the media necessary. In dev it is self-contained and I don't have to flip any bits in the settings except for DEBUG, which I would be doing anyways.

这样,我正在处理的任何项目都有一个site_media包含所有必要媒体的目录。在开发中,它是独立的,我不必翻转设置中的任何位,除了DEBUG,无论如何我都会这样做。

回答by robertmoggach

The Django docs recommend the following approach I've modified for my use case:

Django 文档推荐了我为我的用例修改的以下方法:

urlpatterns = [
    # url patterns
]

from django.conf import settings

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Note: the above assumes you've set your MEDIA_URLand MEDIA_ROOTcorrectly

注:上述假设你将自己MEDIA_URLMEDIA_ROOT正确

... and here's the djangodocs linkslap.

...这是djangodocs linkslap