Django and CSS

11 views
Skip to first unread message

Bobo

unread,
Jul 28, 2008, 5:45:20 AM7/28/08
to Django users
Hi all.

I'm new to Django and I'm working on a project for my work. Most of
the code is done so now I need to style it up and make it more
readable for the users.

I'm a bit confused about how the hole CSS / media_root / media_url /
urls works. I've read the scanty information in the Django Book(The
definitive guide to Django) and I've read the Django Documentation at
http://www.djangoproject.com/documentation/static_files/, but I'm
still somewhat confused.

Fist some info: The project is running on a CentOS Linux machine with
a MySQL database.

I've tried to define the media url and root and have wrote like this:

MEDIA_ROOT = '/home/normskema/norm2/norm2/templates/statics/'

MEDIA_URL = 'http://localhost:8000/media/'

In my URLS I've wrote:

(r'^http://localhost:8000/(?<path>.*)$', 'django.views.static.serve',
{'/home/normskema/norm2/norm2/templates/statics/css/default.css': '/
home/normskema/norm2/norm2/templates/statics/'}),

I'm pretty sure that it's incorrect, but how do I write the correct
path so I can use CSS files and JS files in my templates?

Thanks
- Bobo

Julien Phalip

unread,
Jul 28, 2008, 10:13:27 AM7/28/08
to Django users
Hi,

Maybe you should try with something like:

MEDIA_ROOT = '/home/normskema/norm2/norm2/templates/statics/'
MEDIA_URL = '/media/'

In the URLConf:

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

Hope it helps.

Julien

On Jul 28, 7:45 pm, Bobo <edevan...@gmail.com> wrote:
> Hi all.
>
> I'm new to Django and I'm working on a project for my work. Most of
> the code is done so now I need to style it up and make it more
> readable for the users.
>
> I'm a bit confused about how the hole CSS / media_root / media_url /
> urls works. I've read the scanty information in the Django Book(The
> definitive guide to Django) and I've read the Django Documentation athttp://www.djangoproject.com/documentation/static_files/, but I'm

Tim Sawyer

unread,
Jul 28, 2008, 2:44:18 PM7/28/08
to django...@googlegroups.com
Here's what I use:

in urls.py:

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

This only works with the local test server, see
http://www.djangoproject.com/documentation/modpython/#serving-media-files for
doing it in production with mod_python

in settings.py:

MEDIA_ROOT = '/home/me/web/site/site_media/'
MEDIA_URL = 'http://localhost:8000/site_media/'

/home/me/web/site is my root package for django (so settings.py is in
site/settings.py)

I have css files etc inside site_media and I refer to them in my html using
a /site_media prefix, so

<link rel="stylesheet" href="/site_media/css/site.css" type="text/css">

Hope that helps,

Tim.

jgomo3

unread,
Aug 27, 2008, 12:58:48 PM8/27/08
to Django users
I have no problem serving static content, but i'm still confused about
the MEDIA_ROOT and MEDIA_URL.

Why the ROOT if i have the URL?
how do you actually use those variables?

In production and in develpment server i havent used them, i simply
serve the statics content in some virtual server for that (avoiding
the use of django.views.static.serve becuase it force many changes
when passing thr project from the develpment server to apache).

Are they used to avoid hard code the URL when referencing to things
like css? (example, to avoid 'href="http://example.com/css/site.css"'
and use 'href="http://%s/css/site.css' % MEDIA_URL).
If that then:
Why the MEDIA_ROOT?
For accessing the media files vie the filesystem on views or
someting?
If that then:
It is confusing the comment on the setting file in the variable
MEDIA_URL wich says "This URL handles the media served from
MEDIA_ROOT" because the media files accessed via the MEDIA_URL doesn't
have to be in the same machine. So, is intended that the two variables
refer to the same files?

Juan Miguel

unread,
Aug 27, 2008, 1:19:24 PM8/27/08
to django...@googlegroups.com
MEDIA_URL: Says what is the RELATIVE PATH to media files. So it is relative to the root dir in server. Used when referring links in templates. I.E: http://{{MEDIA_URL}}img/img.jpg. It has many other uses but the most important is the one before.

MEDIA_ROOT: Says what is the ABSOLUTE PATH to media files. So, it is relative to a LOCAL dir (in server or not). Used to say the exact dir (not relative to any server) where media is.

I hope u to understand the difference better.

Up Django!

2008/8/27 jgomo3 <jgo...@gmail.com>



--
-----------------------
Juan M.A.N.
-----------------------

varikin

unread,
Aug 28, 2008, 2:35:40 PM8/28/08
to Django users
MEDIA_ROOT is the local path to the files on the server. One place
this is used is with uploading files. So uploaded files will be
placed in MEDIA_ROOT/some-upload-dir. I don't know about other uses,
though.

John

jgomo3

unread,
Aug 29, 2008, 12:51:03 PM8/29/08
to Django users
Juan Miguel: Confirm my suspects, thankyou.
varikin: how i didn't think on it before, thanyou.
Reply all
Reply to author
Forward
0 new messages