css w/ django

15 views
Skip to first unread message

Clark

unread,
Mar 16, 2012, 7:15:10 PM3/16/12
to Django users
Trying to get django pages to recognize a css file....having trouble
getting all of the correct settings right in the settings.py, urls.py
etc...plus there appears to be several different ways to do this.

I'm on a Mac OSX and I've tried this:

settings.py:
MEDIA_ROOT = 'full-path-to-site-on-machine/documents/mysite/mysite/
media/'
MEDIA_URL = 'media'

urls.py:

from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead, about
from mysite import settings

urlpatterns = patterns('',
(r'^hello/$', hello),
(r'^time/$', current_datetime),
(r'^time/plus/(\d{1,2})/$', hours_ahead),
(r'^about/$', about),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)

base.html
<link rel="stylesheet" href="/media/style_sheets/style.css" type="text/
css" />

Thanks

Ahmad Abd Arrahman

unread,
Mar 16, 2012, 7:30:23 PM3/16/12
to django...@googlegroups.com
here is my config that works fine so far may help you

#settings.py
from os.path import abspath

ROOT_PATH = abspath(dirname(__file__))

MEDIA_ROOT = ROOT_PATH + '/../media/'
#remember to end this with a trailing slash that you forgot in your settings

STATIC_ROOT = ROOT_PATH + '/../static/'
STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
    
#   but any folders that may hold static files like css of js you may use in your project
    ('bootstrap', ROOT_PATH + '/bootstrap/'), 
    #so u can refer to static files in templat using ie <a href="{{ STATIC_URL}}bootstrap/css/bootsrap.css">
)

and I don't use the 

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

although every thing works for me without it
Reply all
Reply to author
Forward
0 new messages