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?

查看:249
本文介绍了MEDIA_ROOT,MEDIA_URL,STATIC_ROOT,STATIC_URL和ADMIN_MEDIA_PREFIX的文档定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过官方文档和一些帖子阅读了关于他们的内容,但我仍然感到困惑。就我现在可以看到,MEDIA_ROOT是用于用户上传的图像和文件,STATIC_ROOT用于js,css等。对于MEDIA_URL,是用于检索图像吗?并且是STATIC_URL用于链接js和css?



如果为每个例子提供了例子,我将非常感激。

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆