6

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!

Mark Irvine
  • 1,349
  • 14
  • 24
  • "The documentation of this is a bit confusing" would replace "The web framework for perfectionists with deadlines" at the top of every djangoproject.com page if false advertising laws applied to FOSS. – Darien Marks Jun 16 '20 at 01:10

2 Answers2

5

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.

Silver Light
  • 44,202
  • 36
  • 123
  • 164
  • hmm... no I started it on port 80 with runserver 0.0.0.0:80 – Mark Irvine May 21 '11 at 21:45
  • 1
    The absolute URL is not just not needed but it won't work at all. If you set STATIC_URL = 'http://localhost:8000/static/' it will produce weird errors, like broken pipe and 404s for the static files. The reason why this happens is unknown for me but little bit about this behaviour is mentioned in the docs at https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development – Akseli Palén Feb 28 '13 at 02:11
2

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.

Mark Irvine
  • 1,349
  • 14
  • 24