MEDIA_ROOT,MEDIA_URL,STATIC_ROOT,STATIC_URL和ADMIN_MEDIA_PREFIX的文档定义是什么? [英] What is the documented definition of MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL and ADMIN_MEDIA_PREFIX?
问题描述
如果为每个例子提供了例子,我将非常感激。
MEDIA_ROOT和STATIC_ROOT是文件所在的本地目录,例如:
MEDIA_ROOT ='/ home / CDBean / mydjangoproject / media /'#注意尾部斜杠
STATIC_ROOT ='/ home / CDBean / mydjangoproject / static /'
MEDIA_URL和STATIC_URL是这些文件夹的公开可访问的URL。这意味着当您部署Django项目时,您必须告诉您的网络服务器在这里指定的URL下发布这些文件夹。
MEDIA_URL ='http://media.example.com/'#尾部斜杠在这里也
STATIC_URL ='http://static.example.com/'
然后您可以使用这些URL(假设您有 django.core.context_processors.media
和 django.core.context_processors.static
通过{{MEDIA_URL}}和{{STATIC_URL}}添加到模板中的settings.py中的TEMPLATE_CONTEXT_PROCESSORS元组中)。两个例子:
< link href ={{STATIC_URL}} css / main.cssmedia =screen =stylesheettype =text / css/>
< img src ={{MEDIA_URL}} random.jpg/>
现在,什么时候使用什么?基本上你是对的,但我强烈建议您阅读 https://docs.djangoproject .COM / EN的/ dev / HOWTO /静态文件/ 。
I have read something about them via official doc and some posts, but I'm still confused. As far as I now can see, MEDIA_ROOT is for user uploaded images and files and STATIC_ROOT for js, css, etc. As for MEDIA_URL, is that for retrieving images? And is STATIC_URL for linking js and css?
I would appreciate it very much if examples are provided for each.
MEDIA_ROOT and STATIC_ROOT are the local directory the files reside in, for example:
MEDIA_ROOT = '/home/CDBean/mydjangoproject/media/' # notice the trailing slash
STATIC_ROOT = '/home/CDBean/mydjangoproject/static/'
MEDIA_URL and STATIC_URL are the publicly reachable URLs of those folders. That means that when you deploy your Django project, you'll have to tell your web server to publish those folders under the URLs you specify here.
MEDIA_URL = 'http://media.example.com/' # trailing slashes here, too
STATIC_URL = 'http://static.example.com/'
You can then use those URLs (assuming you have django.core.context_processors.media
and django.core.context_processors.static
added to the TEMPLATE_CONTEXT_PROCESSORS tuple in settings.py) in your templates via {{MEDIA_URL}} and {{STATIC_URL}}. Two examples:
<link href="{{STATIC_URL}}css/main.css" media="screen" rel="stylesheet" type="text/css" />
<img src="{{MEDIA_URL}}random.jpg"/>
Now, when to use what? Basically you are right, but I strongly recommend reading https://docs.djangoproject.com/en/dev/howto/static-files/.
这篇关于MEDIA_ROOT,MEDIA_URL,STATIC_ROOT,STATIC_URL和ADMIN_MEDIA_PREFIX的文档定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!