The exact method of adding media resources to a Django application will
depend on a number of factors (OS, Webserver, project layout) but it's
fairly straight-forward.  Here's the approach that I use:

Assume that my Django project is named 'foo' and that any media (CSS,
Javascript, etc) files I want to reference are in foo/media:

In settings.py, I will make the following changes:

import os

SITEDIR = os.path.dirname( os.path.dirname( os.path.abspath( __file__ ) ) )
ADMIN_MEDIA_PREFIX = '/media'
MEDIA_URL = '/m/'
MEDIA_ROOT = os.path.join( SITEDIR, 'foo', 'media' )
LOCAL_MEDIA = True

In urls.py, I will make the following changes:

from django.conf import settings

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

At this point all you need to do is to put your CSS or other files into the
foo/media directory, and then reference them relative to /m/.

This will be fine for using Django's built-in runserver for testing.  If
you're in production with a webserver, then you'll want to map /m/ to the
actual directory using the webserver's directives, and then set LOCAL_MEDIA
to False.  This will allow the webserver to serve static files without
Django getting into the mix.  The exact configuration you use will depend
upon your webserver and/or WSGI configuration.  You may need to adjust the
number of dirname() calls in setting up the SITEDIR variable to match your
local configuration; this is what I use for my simple Django sites.

When you compare this to the configuration that you quoted, the MEDIA_URL
should be absolute, but shouldn't reference a hostname or port, the
urlpattern entry should define the document_root parameter, and your HTML
documents should reference an absolute, not relative path (/media/..., not
media/...).

- Craig -

On Fri, Feb 4, 2011 at 17:33, h@ck5t0ck <hackstock...@gmail.com> wrote:

> Hi,
> Thanks for your quick reply. I tried what you showed me but it didn't
> work. Below are the contents of my settings.py and urls.py files.
> Could you please study them and point out where am going wrong?
>
> SETTINGS.PY
> ===========================================================
> import os
>
> DEBUG = True
> TEMPLATE_DEBUG = DEBUG
>
> ADMINS = (
>    # ('Your Name', 'your_em...@domain.com'),
> )
>
> PROJECT_DIR = os.path.dirname(__file__)
>
> MANAGERS = ADMINS
>
> DATABASES = {
>    'default': {
>        'ENGINE': 'django.db.backends.mysql', # Add
> 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
>        'NAME': 'django_bookmarks',                      # Or path to
> database file if using sqlite3.
>        'USER': 'root',                      # Not used with sqlite3.
>        'PASSWORD': 'toor',                  # Not used with sqlite3.
>        'HOST': 'localhost',                      # Set to empty
> string for localhost. Not used with sqlite3.
>        'PORT': '3306',                      # Set to empty string for
> default. Not used with sqlite3.
>    }
> }
>
> # Local time zone for this installation. Choices can be found here:
> # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
> # although not all choices may be available on all operating systems.
> # On Unix systems, a value of None will cause Django to use the same
> # timezone as the operating system.
> # If running in a Windows environment this must be set to the same as
> your
> # system time zone.
> TIME_ZONE = 'America/Chicago'
>
> # Language code for this installation. All choices can be found here:
> # http://www.i18nguy.com/unicode/language-identifiers.html
> LANGUAGE_CODE = 'en-us'
>
> SITE_ID = 1
>
> # If you set this to False, Django will make some optimizations so as
> not
> # to load the internationalization machinery.
> USE_I18N = True
>
> # If you set this to False, Django will not format dates, numbers and
> # calendars according to the current locale
> USE_L10N = True
>
> # Absolute path to the directory that holds media.
> # Example: "/home/media/media.lawrence.com/"
> MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
>
> # URL that handles the media served from MEDIA_ROOT. Make sure to use
> a
> # trailing slash if there is a path component (optional in other
> cases).
> # Examples: "http://media.lawrence.com";, "http://example.com/media/";
> MEDIA_URL = 'http://127.0.0.1:8000/media/'
>
> # URL prefix for admin media -- CSS, JavaScript and images. Make sure
> to use a
> # trailing slash.
> # Examples: "http://foo.com/media/";, "/media/".
> ADMIN_MEDIA_PREFIX = '/media/'
>
> # Make this unique, and don't share it with anybody.
> SECRET_KEY = 'xjz$(&oq0oz_c!3(%nkzp%@0zv)^!-c#2tbcqiix*bpu1303ml'
>
> # List of callables that know how to import templates from various
> sources.
> 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',
> )
>
> ROOT_URLCONF = 'django_bookmarks.urls'
>
> TEMPLATE_DIRS = (
>    os.path.join(PROJECT_DIR,'templates')
> )
>
> INSTALLED_APPS = (
>    'django.contrib.auth',
>    'django.contrib.contenttypes',
>    'django.contrib.sessions',
>    'django.contrib.sites',
>    'django.contrib.messages',
>    'django.contrib.admin',
>    'django_bookmarks.bookmarks',
>    # Uncomment the next line to enable admin documentation:
>    # 'django.contrib.admindocs',
> )
>
> URLS.PY
> ===================================
> from django.conf.urls.defaults import *
> from django.contrib import admin
> from bookmarks.views import main_page
> admin.autodiscover()
>
> urlpatterns = patterns('',
>     (r'^$',main_page),
>     (r'^admin/', include(admin.site.urls)),
>     (r'^media/(?P<path>.*)$','django.views.static.serve'),
> )
>
> MAIN_PAGE.HTML
> =====================================
> <html>
> <head>
> <title>{{head_title}}</title>
> <link rel="stylesheet" type="text/css" href="media/css/layout.css"/>
> </head>
> <body>
> <h1>{{page_title}}</h1>
> <p>{{page_body}}</p>
> </body>
> </html>
>
> On Feb 4, 10:13 pm, ozgur yilmaz <yelb...@gmail.com> wrote:
> > In your template:
> >
> > <link href="/site_media/css/style.css" media="screen" rel="stylesheet"
> > type="text/css" />
> >
> > In your urls.py:
> >
> > site_media = os.path.join(os.path.dirname(__file__),'site_media')
> >
> > urlpatterns = patterns('',
> >             (r'^site_media/(?P<path>.*)$' ,
> > 'django.views.static.serve',{'document_root':site_media}),
> > )
> >
> > Maybe helps...
> >
> > 2011/2/4 h@ck5t0ck <hackstock...@gmail.com>
> >
> > > Hi guys,
> > > am very new to django and am having troubles adding css to my pages
> > > even though i've found some tutorials online
> > > and followed it word for word.
> > > Can anyone please help me urgently? I really appreciate your help.
> > > Best Wishes,
> > > h@ck5t0ck
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Django users" group.
> > > To post to this group, send email to django-users@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> <django-users%2bunsubscr...@googlegroups.com<django-users%252bunsubscr...@googlegroups.com>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to