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?