0

I'm having a very similar problem to this question but unfortunately none of the solutions seem to be working for me.

Essentially I have the following, really simple directory:

ulogin
 - models.py
 - tests.py
 - views.py
 - media 
   - screen.css
 - templates
   - utemplate
     - index.html

in my settings.py I have the following defined:

MEDIA_ROOT = '../compwebsite/ucode/ulogin/media'
MEDIA_URL = '/media/'

and in index.html I have the following trying to reference screen.css

<html>
<head> 
    <title>
        YAY TITLE
    </title>
    <link rel="stylesheet" href="{{ MEDIA_URL }}screen.css">
<!--and it more...-->

My urls.py I have the following:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

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

No matter what I do, I can't get the reference to the screen.css to work. I'm really uncertain of how to fix this based on the other questions here on the site. Thank you so much and let me know if you need more info!

Community
  • 1
  • 1
gersande
  • 465
  • 1
  • 8
  • 25

1 Answers1

1

Static files should be placed in the 'static' directory; this is where Django will look. The 'media' directory is meant to hold user-uploaded files, which get copied over to the static directory when the django server runs.

Change your MEDIA_URL references to STATIC_URL

You shoudn't have to use the URLConf to manage static files at all.

As for your Django settings, it's not a good idea to hard code absolute paths in case they are changed (i.e. when pushed to a deployment server).

A better idea is to do something like this:

from os import path

PROJECT_ROOT = path.abspath(path.dirname(path.dirname(__file__)))

MEDIA_ROOT = path.join(PROJECT_ROOT, 'media/')

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    path.join(PROJECT_ROOT, 'static/'),
)

This will ensure that Django is using the correct absolute path regardless of where your project is on your system.

You can also enable the DefaultStoreFinder in STATICFILES_FINDERS although it shouldn't be necessary.

irvanjitsingh
  • 1,082
  • 1
  • 12
  • 19