访问“媒体” Django中的文件 [英] Accessing "Media" files in Django

查看:160
本文介绍了访问“媒体” Django中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要爱Django,但在开发环境中这种静态和媒体文件的业务正在推动我。请救我脱离我的愚蠢。



我在我的开发机器上。我的项目目录根目录下有文件夹 media



settings.py 我有: MEDIA_ROOT ='' MEDIA_URL ='/ media /'



urls.py 我有:

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

但是,我可以通过引用 / media / media / 例如
< img src =/ media / media / image.png/>



我期待(而且想要)

< img src =/ media / image.png/>



任何人都可以告诉我这里发生了什么,并给我一个简单的配方来设置媒体文件处理?



非常感谢。






@Timmy O'Mahony - 谢谢!史诗篇,非常清楚。但是它留下了几个问题:



(1)我必须使用 / media / / static / ,而不是 media / static / as MEDIA_URL STATIC_URL - 我错过了什么?



(2)如果 collectstatic 软管 / static / 你在哪里放置网站级的CSS例如该网站的CSS文件?不在 / static / 中。



(3)我把它们放在项目根目录_,并设置 STATICFILES_DIRS 指向它 - 这似乎是开发服务器获取其静态文件的地方,尽管 urlpatterns 指令。如果这是错误的,您在开发过程中放置​​网站级CSS,在哪里修改 collectstatic 之前的工作流程 - 您是否必须编辑它们一个地方,在每次编辑之后将它们收集在别的地方?

解决方案

为什么你将 MEDIA_ROOT 设置为空白?它需要是您的媒体目录的路径。因为,正如你所说,你的媒体在一个名为 media 的子目录中,你应该把它放在 MEDIA_ROOT 中。 p>

I'd like to love Django, but this business of static and media files in development environments is driving me nuts. Please rescue me from my stupidity.

I'm on my development machine. I have folder media in the root of my project directory.

In settings.py I have: MEDIA_ROOT = '' and MEDIA_URL = '/media/'.

In urls.py I have:

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

But the only way I can get media files is by referencing /media/media/ e.g. <img src="/media/media/image.png" />.

I expect (and want)
<img src="/media/image.png" />

Can anyone tell me what is happening here, and give me a simple recipe for setting up media file handling?

Thank you very much.


@Timmy O'Mahony - thanks! epic post, and very clear. But it leaves a couple of questions:

(1) I have to use /media/ and /static/, not media/ and static/ as MEDIA_URL and and STATIC_URL - am I missing something?

(2) If collectstatic hoses /static/, where do you put site level CSS e.g. the site's CSS files? Not in /static/, evidently.

(3) I put them in a directory '_' off the project root and set STATICFILES_DIRS to point to it - and that seems to be where the development server gets its static files, despite the urlpatterns directive. If THAT is wrong, where do you put site level CSS during development, and what is the workflow around collectstatic when you modify them - do you have to edit them one place, and collect them someplace else after every edit?

解决方案

Why have you made the MEDIA_ROOT setting blank? It needs to be the path to your media directory. Since, as you say, your media is in a subdirectory called media, you should put that in MEDIA_ROOT.

这篇关于访问“媒体” Django中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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