Search code examples
pythoncssdjangodjango-admindjango-staticfiles

djagno admin css is not working after upgrading django to 1.6


I have two django apps one with django version 1.3.1 and other is django 1.2.3 and both frontend and admin section css files are serving/working fine. But recently i have upgraded both apps to django 1.6. Now after upgrading frontend site(css) is work fine but admin css is not working/serving, i don't know what went wrong after upgrading. May be some static serving structure has been changed in django latest version ? and below are some of my settings file options

django app one settings

SETTINGS_DIR = os.path.dirname(__file__)
DJANGO_PROJ_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, os.path.pardir))
SITE_DIR = os.path.abspath(os.path.join(DJANGO_PROJ_DIR, os.path.pardir))
HOME = DJANGO_PROJ_DIR
STATIC_DOC_ROOT= HOME + '/static/'
MEDIA_ROOT = HOME + '/media'
MEDIA_URL = '/media/'

ADMIN_MEDIA_PREFIX = '/media/'
ADMIN_ROOT = '/admin/'


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.contrib.messages.context_processors.messages",
"django.core.context_processors.request")

# List of callables that know how to import templates from various sources.
if DEBUG:
    TEMPLATE_LOADERS = [
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    ]
else:
    TEMPLATE_LOADERS = [
        ('django.template.loaders.cached.Loader',(
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',
            # 'forum.modules.template_loader.module_templates_loader',
            # 'forum.skins.load_template_source',
            )),
    ]

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

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.
    HOME + '/templates',
)

django app two settings

MIDDLEWARE_CLASSES = (                      
    'django.middleware.gzip.GZipMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'pagination.middleware.PaginationMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
  'django.contrib.auth.context_processors.auth',
  'django.core.context_processors.debug',
  'django.core.context_processors.media',
  'django.core.context_processors.request',
 )

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

MEDIA_URL = '/media/'

ADMIN_MEDIA_PREFIX = '%s/media/admin/' % MEDIA_URL

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
)

So can someone know how to fix this ? and make the admin css work ?


Solution

  • To get the static files to work, a few things need to be done. It depends on what you're particular problem is. Try the following:

    1. Look in your web server's error log to see what's going wrong from your web server's perspective. If the error complains about missing files, then you may have mis-configured your static file settings, or you might have permissions issues.

    2. When you browse to a page in the offending app, view the source for it, and take note of the url that it points to. Is it pointing to the correct place? If not, then you have an issue with your project or app settings (or in your template).

    My best guess is that you have a problem with your project's settings. It looks like your missing these lines from your project's settings.py file:

    STATIC_URL= '/some/relative/url/' 
    STATIC_ROOT = '/path/to/your/project/static/'
    STATICFILES_DIRS = ('path/to/your/project/static/',)
    

    The first determines the url that should be used to reference css and other static files (such as in html output by your app), and the second and third designate the actual directory on your server's file system where the files can be found.

    For this to work, your webserver has to map STATIC_URL to STATIC_ROOT. If you are using apache, that would involve putting the following directive within the VirtualHost block:

    Alias    /static/    /path/to/your/project/static/
    

    Furthermore, apache (or rather, the user that runs apache) must have read access to the static directory!

    If this separation of static files is new to you, you should read this.

    You shouldn't physically put your static files (such as css) inside the STATIC_ROOT directory. Instead, put them inside their respective app's static folder. Suppose you put some css files inside /path/to/your/app_name/static/css/. In your project, run python manage.py collectstatic, and Django will copy the css files to /path/to/your/project/static/app_name/css/. Your STATIC_ROOT is what determines where they go.

    If you are still stuck, please check your web server error log, and post back with the django and/or web server errors you're getting.