Ska
Ska

Reputation: 6888

Django admin media stopped being served locally

I set up a simple project and everything was working fine up to a certain point. I was adding stuff to the project, paths to static dirs, etc, and then suddenly I realised that admin media stopped being served, no css, no images.

Viewing the source of the page reveals this:

link rel="stylesheet" type="text/css" href="/static/admin/css/base.css"

Which is correct and the same path works with any newly created Django project.

Basically I would like to somehow reenable that the admin media is being served from default location.

This is the settings.py but frankly I wasn't changing it much from the point when it stopped working.

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "site_media", "media")

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = "/site_media/media/"

# Absolute path to the directory that holds static files like app media.
# Example: "/home/media/media.lawrence.com/apps/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static1")

# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/site_media/"

# Additional directories which hold static files
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "site_media"),
    os.path.join(PROJECT_ROOT, "site_media", "static"),
    os.path.join(PROJECT_ROOT, "site_media", "media"),
]

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '__@9nw29=7gbj8xb5z*u6cew3x8m(&_v&jlp16!^bnpe+6@w0#'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'wizs.urls'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, "templates"),
)


TEMPLATE_CONTEXT_PROCESSORS = [
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "django.contrib.messages.context_processors.messages",
    'django.core.context_processors.static',
]

UPDATE

Seems that it came down to juggling these two properties:

1) Now I see admin media but NOT the media (e.g. uploaded images)

STATIC_URL = "/site_media/static/"
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'

2) Now I see uploaded media files but NOT the admin media

STATIC_URL = "/site_media/"
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'

Upvotes: 0

Views: 797

Answers (1)

Issac Kelly
Issac Kelly

Reputation: 6359

It looks like STATIC_URL should be "/site_media/static1/" and ADMIN_MEDIA_PREFIX should be "/site_media/static1/admin/"

Some other suggestions:

  1. Don't compile your MEDIA_ROOT into your STATIC_ROOT, so remove os.path.join(PROJECT_ROOT, "site_media", "media"),
  2. Your local static files should also not be in site_media. so remove os.path.join(PROJECT_ROOT, "site_media", "static"), and change it to os.path.join(PROJECT_ROOT, "static"), and move that directory there.
  3. You should the clear out site_media locally, and only have it filled on the server. (or, of course with any media that you upload locally). But you don't need to run collectstatic locally at all.
  4. If/Once you do those things, you can change "static1" back to static, and then serve the entire /site_media/ folder via nginx, and then you won't have any duplicates in that folder.

Good luck.

Here Are my settings:

# settings.py
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'site_media', 'media')
MEDIA_URL = '/site_media/media/'

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'site_media', 'static')
STATIC_URL = '/site_media/static/'

ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

# urls.py
from django.conf import settings

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

Upvotes: 1

Related Questions