4
votes

Just upgraded to Django 1.4, and having serious trouble with the new 'improved' serving of static and media files on development server. I love Django, but why on earth they have made serving these files doubly more complicated with STATIC_URL,STATIC_ROOT, STATICFILES_DIR now is utterly beyond me.

I'm simply trying to serve all files, static and uploaded, on the development server. I have the STATIC_URL files working, after much experimentation, but I simply cannot get the MEDIA_URL files to be served as well.

Settings:

DIRNAME = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(DIRNAME, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()

I've got the media and static context processors added:

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
'django.core.context_processors.media',
'django.core.context_processors.static',
"django.core.context_processors.request",
'satchmo_store.shop.context_processors.settings',
'django.contrib.messages.context_processors.messages',

)

and I've added in the url confs:

# serve static and uploaded files in DEV
urlpatterns += staticfiles_urlpatterns()
urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

with the two conf settings added as indicated in the docs, first one for static, second for media.

my structure, website being an app and static dir placed inside it as instructed on djangoproject:

<myproject>
  --media
  --settings
  --templates
  --website
      |->static

In templates I can serve static content no problem with

{{STATIC_URL}}css/style.css

But any uploaded image, this one using photologue, is not served, but the urls are correct:

/media/photologue/photos/cache/spawning-2_admin_thumbnail.jpg

That directory structure does exit under media/

Super, super confused. It all seems so ridiculously complicated now, whereas I never had any issues before.

3
I know that feel, I just gave up and used mod_wsgi and Apache to circumvent Django's whole way of doing it. - Liam Bigelow
I should add I've got the staticfiles app installed and DEBUG = True, because I can serve static files.. - professorDante
should it be urlpatterns += static(... not just plus? - Enrico
changed it to the old way, as per @username below:urlpatterns += (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), ) but still no joy. - professorDante

3 Answers

2
votes

I´m very new to Django and I´ve never had any problem with static content.

This is my configuration. I hope it can help

My folder structure

django-project
--mainapp
----settings.py
----wsgi.py
----[...]
--otherapp
--fixtures
--static
--templates
--manage.py
--requirements.txt

settings.py

import os, socket

DEBUG = True
TEMPLATE_DEBUG = DEBUG
MAIN_APP = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.join(MAIN_APP, ".."))
MY_LOCALHOST = "VirusVault.local" # this is the real name of my local machine :)

try: HOST_NAME = socket.gethostname()
except: HOST_NAME = "localhost"

[...]

if HOST_NAME == MY_LOCALHOST:
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
    STATIC_URL = "/static/"
    MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media/')
    MEDIA_URL = "/media/"
else:
    STATIC_ROOT = "/server/path/to/static/files"
    STATIC_URL = "http://server.com/static/"
    MEDIA_ROOT = "/server/path/to/static/files/media/"
    MEDIA_URL = 'http://server.com/static/media/'

You need to add 'django.contrib.staticfiles' to INSTALLED_APPS

urls.py

if settings.HOST_NAME == settings.MY_LOCALHOST:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    )
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
    )
0
votes

If you are using Django 1.4 folder structure, you've moved settings.py to your new website app folder which means your MEDIA_ROOT is now incorrect. Not sure if a relative location works in this case, but it should be something like this

MEDIA_ROOT = os.path.join(DIRNAME, '../media/')

It might be simpler to use an absolute path.

0
votes

I intended to write a comment first but somehow add comment button doesn't work.

Did you check the permissions of media directory?

Since it became an answer, I am going to dump one of my perfectly working Django 1.4 sites configuration.

structure:

-myproject
-- media
-- static
-- templates
-- myproject
--- settings.py
--- urls.py

settings.py:

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) # Not the best way but works
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'

urls.py:

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