6

The media is currently on my local development machine.

My MEDIA_ROOT, MEDIA_URL, ADMIN_MEDIA_PREFIX and are specified as below:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
MEDIA_URL = '/media/'
SITE_URL = 'http://localhost:80'
ADMIN_MEDIA_PREFIX = '/media/admin/'

There is no 'admin' folder but that shouldn't make a difference I don't think. In the urls.py file I have:

(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),

I am at a loss as to what I should do to get it working. [I am trying to learn django and am working with an existing project that's pretty hairy]

Gul Ershad
  • 1,743
  • 2
  • 25
  • 32
StanM
  • 827
  • 4
  • 12
  • 33

4 Answers4

7

works with django 1.8 - 1.11:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development

note that Django documentation states that this is

not suitable for production use

(obviously unless you use if settings.DEBUG: part)

Bob
  • 5,809
  • 5
  • 36
  • 53
  • 1
    This is documented here https://docs.djangoproject.com/en/1.10/howto/static-files/#serving-files-uploaded-by-a-user-during-development, and at least as of 1.10, you don't need the `if settings.DEBUG:` bit. You will still need to conditionally configure `MEDIA_URL` and `MEDIA_ROOT` based on whether you're in debug mode. – Dmitry Minkovsky Mar 30 '17 at 01:30
7

You're mixing and matching pre and post-Django 1.3 static file handling. Originally all static files were served from MEDIA_URL, but Django 1.3 introduced the staticfiles contrib package and the associated STATIC_ROOT and STATIC_URL settings. django.views.static.serve utilizes the new staticfiles app, which you haven't set up.

Assuming you're running Django 1.3, first, you'll need to add 'staticfiles' to your INSTALLED_APPS. Then, you'll need to define STATIC_ROOT and STATIC_URL. The standard location is a project-root level directory named "static".

You'll also need to add the staticfiles template context processor:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.static',
)

This will make the STATIC_URL variable available in your templates, so you can reference your resources with something like {{ STATIC_URL }}css/style.css

All your static resources will also need to go into an app(s)-level directory named "static". The actual project-root level "static" directory is never directly used. It's simply the place where the collectstatic management command dumps all your static resources for use in production.

If you want project-wide static resources (not tied to any one particular app), you'll need an entirely separate directory (i.e. not the same as MEDIA_ROOT or STATIC_ROOT). I tend to use one named "assets". You'll then need to tell Django to look in here for static resources as well with the STATICFILES_DIRS setting:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), 'assets'), # or whatever you named it
)

MEDIA_ROOT/MEDIA_URL are now only used for user uploads (e.g. any file created through FileFields and ImageFields, so you still need it, but you won't ever manually store anything there.

When you reach production, your webserver will need to serve both MEDIA_ROOT and STATIC_ROOT at MEDIA_URL and STATIC_URL, respectively. You'll also need to run:

$ python manage.py collectstatic

To make Django compile all your static files into the directory specified by STATIC_ROOT.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Oof, should have remembered I was working in 1.3 when the code is from 1.2, Is there a way for me to not have to change too much in 1.2 code to be able to use that directory without a problem? Or would it be better for me to downgrate to django 1.2? – StanM Feb 06 '12 at 22:54
  • There's a few other settings, such as `DATABASES` that may give you deprecation warnings or otherwise may need to be modified for compatibility with Django 1.3, but there's relatively few mandatory changes overall. Personally, I'd recommend sticking with 1.3 and making the app work. The slight work potentially involved in making it work is far outweighed by the benefits of things like staticfiles, class-based views, improved template tags, etc. Plus, it will make it easier to eventually jump to 1.4, which will nearly be a must have release from what I've seen. – Chris Pratt Feb 07 '12 at 15:55
4

On development server, this page may help you. https://docs.djangoproject.com/en/1.2/howto/static-files/

By adding follow code to urls.py:

if settings.DEBUG:
    urlpatterns += patterns('', 
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
    )
hirokiky
  • 156
  • 5
0

With python-django 1.7 I used

 if settings.DEBUG:
        urlpatterns = patterns('',
            (r'^$', 'blenderx3d.first_step.views.index'),
            (r'^media/(?P<path>.*)$','django.contrib.staticfiles.views.serve'),)