0

Possible Duplicate:
Django and Serving Static Files

I am with one problem to load CSS at base.html. I put all css files at the directory /static.

At urls.py I put this code:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
           { 'document_root': '/home/bkcherry/botstore/botstore/static' }),
    )

And at base.html i put the following:

<link rel="Stylesheet" type="text/css" href="/static/css.css" />

When I go to the main.html, the css style is not working. I need to configure the settings.py MEDIA_ROOT, MEDIA_URL or STATIC_ROOT?

Community
  • 1
  • 1
  • 1
    what happens if you just point your browser to http://whatever.com/static/css.css ? – Joran Beasley Oct 16 '12 at 15:55
  • This could help, too. Also don't miss to pass RequestContext. http://stackoverflow.com/questions/12819395/how-to-make-my-css-files-to-work-in-django/12821074#12821074 – Thomas Schwärzl Oct 16 '12 at 16:06

3 Answers3

1

you must not use MEDIA_ROOT or MEDIA_URL this is for uploaded media not your static content, and you do not need to setup URL patterns as that is only for django 1.2 or " if you are using some other server for local development": https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development

you need to have your static files in: botstore/botstore/static/botstore/css.css

then use:

HOME_ROOT = os.path.dirname(__file__)

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"

STATIC_ROOT = os.path.join(HOME_ROOT, 'staticfiles')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

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

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

then in your HTML you can refer to your static files thus:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}botstore/css.css" />
Thomas Grainger
  • 2,271
  • 27
  • 34
0

I think you need a slash at the end of your path, ie '/home/bkcherry/botstore/botstore/static/'

hexist
  • 5,151
  • 26
  • 33
0

If you check official documentation

from django.conf import settings

# ... the rest of your URLconf goes here ...

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

MEDIA_ROOT should have / on the end (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-MEDIA_ROOT)

FrEaKmAn
  • 1,785
  • 1
  • 21
  • 47
  • Thx all, it's working now!! ;) I fix the code at urls.py: if settings.DEBUG: urlpatterns += patterns('', url(r'^static/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), ) And i configure the settings.py: MEDIA_ROOT = '/home/bkcherry/botstore/botstore/static/' MEDIA_URL = '/static/' STATIC_ROOT = '' STATIC_URL = 'http://localhost:8000/static/' And now the method GET found the directory. – user1750551 Oct 16 '12 at 16:53