Static files don't load in django

4.2k Views Asked by At

I have stopped creating new project with django 1.1 and from that time was only working on already created applications. Since then I guess that static serving somehow changed (project is using Django 1.2.4). I'm struggling for few hours and with no results, so if somebody knows what I'm doing wrong please let me know.

My settings :

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'

SECRET_KEY = '(d9bahjuyy_i-)4b(w9gc5a&s&5jemcn7&b^wrbuemah3p-6^#'

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',
)

TEMPLATE_DIRS = (
    os.path.join(PROJECT_PATH, 'templates'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'project',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.request",
    "django.core.context_processors.media",
    "django.core.context_processors.csrf",
    "django.core.context_processors.i18n",
)

urls:

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

And media files are located in ../project_path/media

In templates I get the {{ MEDIA_URL }} path as /static/ but my files are not loaded. Going to http://127.0.0.1:8000/static/ (with or without last slash) shows root page. Firebug shows the html code of page in place of javascript files. I would rather expect 404 error. Where's the problem ?


Swicthed to 1.3 . Problem remains.

2

There are 2 best solutions below

0
On

Just in case if you are using render_to_response, you have to pass optional 3rd parameter:

context_instance=RequestContext(request)

soe the full code looks like:

return render_to_response('index.html',{'dict':'ionary'},context_instance=RequestContext(request))

{{ STATIC_URL }} works with RequestContext, while default for render_to_response is Context

0
On

If you are using Django's 1.3 django.contrib.staticfiles, the app will look for static files under the static sub-folder of all your installed apps. For instance, staticfiles will automatically pick up the following css:

yourproject/
    appone/
        static/
            sample.css

If you are using django.contrib.staticfiles and STATIC_URL = '/static/' in your settings.py, you can easily access the css at:

http://localhost:8000/static/sample.css

For your case, it looks like you have a static folder under your project folder, I will assume the following:

yourproject/
    static/

If you want staticfiles to pick the above, remove the following from urls.py:

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

Add the following to your settings.py:

import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))

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

In your template, you might want to start using STATIC_URL instead of MEDIA_URL unless they are both pointing to the same value.

You can read more about staticfiles in Django 1.3.