Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems linking to static files in Django 1.3

I'm running django 1.3, python 2.7 on windows XP

I'm trying to setup a css in a static folder for my django app.

The template looks like:

<html>
    <head>
        <title>css demo</title>
        <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/my.css" />

    </head>
    <body>

The generated HTML looks like:

<html> 
    <head> 
        <title>css demo</title> 
        <link rel="stylesheet" type="text/css" href="http://localhost/static/css/my.css" /> 

    </head>
...

So it appears that I have everything set up in order to specify where the static files are in the template, and then in the generated html.

But there is noting at 'http://localhost/static/css/my.css'. How do I get it there?

I ran collectstatic like so:

C:\root\workspace\mywebapp\src\mywebapp>c:\Python27\python.exe manage.py collectstatic

You have requested to collect static files at the destination location as specified in your settings file.

This will overwrite existing files.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying 'c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css'

1 static file copied to 'C:/root/workspace/mywebapp/src/mywebapp/static/'.

So I now have my.css in c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css

In my settings, I have:

STATIC_ROOT = 'C:/root/workspace/mywebapp/src/mywebapp/static/'
STATIC_URL = 'http://localhost/static/'

and in my url.py I have:

from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',

    (r'^mywebapp/', include('mywebapp.css_demo.urls')),
)

urlpatterns += staticfiles_urlpatterns()

So if I understand correctly, my css at:

c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css

should no be available at:

http://localhost/static/css/my.css

But instead I see:

Page not found (404)
Request Method: GET
Request URL:    http://localhost/static/css/my.css
Using the URLconf defined in mywebapp.urls, Django tried these URL patterns, in this order:
^mywebapp/
The current URL, static/css/my.css, didn't match any of these.

I also tried:

http://localhost/mywebapp/static/css/my.css
http://localhost/mywebapp/css_demo/static/css/my.css

Am I missing a step here? The documentation on this is a bit confusing. http://docs.djangoproject.com/en/1.3/howto/static-files/ http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

Thanks!

like image 614
Mark Irvine Avatar asked May 20 '11 15:05

Mark Irvine


2 Answers

I bet you have your app running at http://localhost:8000, not http://localhost :)

Change

STATIC_URL = 'http://localhost/static/'

to

STATIC_URL = '/static/'

No need for absolute URL there.

like image 88
Silver Light Avatar answered Oct 04 '22 23:10

Silver Light


I created a folder:

C:\root\workspace\mysite\src\mysite\common

And in my settings, I have:

STATIC_ROOT = 'C:/root/workspace/mysite/src/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    "C:/root/workspace/mysite/src/mysite/common/static",
)

This seems to work.

like image 31
Mark Irvine Avatar answered Oct 04 '22 23:10

Mark Irvine