Django can't see my static files

9,836 views
Skip to first unread message

Gchorn

unread,
Aug 1, 2011, 6:17:28 AM8/1/11
to Django users
Hello All,

I'm using Django to develop a website, and I'm having trouble getting
it to see my static files. I've looked through Django's static file
documentation and the steps I've taken to get Django to see my files
(using a CSS file as an example) are as follows:

First, I created a folder called "static" inside the main app folder
for this project. Then, I added "/static/" to the STATIC_URLS section
of the settings.py file, and I also added the full path to my CSS file
under the STATICFILES_DIRS section of settings.py.

Then I added the line:

from django.template import RequestContext, loader

As well as "context_instance= RequestContext(request)" as a third
argument to the "return render_to_response()" call.

Finally, in my url.py file, I added this line at the top:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

And this line at the bottom (separate from the initial "urlpatterns =
patterns()" call):

urlpatterns += staticfiles_urlpatterns()

In my HTML/Django template, I added the following link to my CSS file:

<link rel="stylesheet" type="text/css" href={{ STATIC_DIRS }} />

But then after all of this, when I perform a "python manage.py
runserver" to preview the website, Django still does not appear to
find the external CSS file. I must still be missing something but I'm
not sure what--does anyone know???

I'm sure I'm missing something very basic but I'm a total beginner so
please be patient with me, I probably need a lot of very specific
instructions...

thanks,
Guillaume

Thomas Orozco

unread,
Aug 1, 2011, 6:45:51 AM8/1/11
to django...@googlegroups.com

If your css file is locatedr in your staticfiles dir, you should use something such as {{ STATIC_URL }}main.css, assuming your CSS file is called main and is located in one of the STATIC_DIRS.

Using staticfiles_dirs in a template makes no sense. Not only your are passing a parameter that has to do with your system configuration and not your urls, but you are also using a directory instead of a file.

In short, it is the static_url parameter that should be passed to form a URL.

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

lokesh

unread,
Aug 2, 2011, 1:57:12 AM8/2/11
to Django users

You can use below in setttings.py, create a folder called static
inside project and u can use it for static files.

STATIC_URL = '/static/'

STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/
static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'C:/uidemo/static/',
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)
lokesh s

On Aug 1, 3:45 pm, Thomas Orozco <g.orozco.tho...@gmail.com> wrote:
> If your css file is locatedr in your staticfiles dir, you should use
> something such as {{ STATIC_URL }}main.css, assuming your CSS file is called
> main and is located in one of the STATIC_DIRS.
>
> Using staticfiles_dirs in a template makes no sense. Not only your are
> passing a parameter that has to do with your system configuration and not
> your urls, but you are also using a directory instead of a file.
>
> In short, it is the static_url parameter that should be passed to form a
> URL.

Guillaume Chorn

unread,
Aug 2, 2011, 4:28:00 AM8/2/11
to django...@googlegroups.com
Thomas and Lokesh,

Thanks very much for your help.  I've modified the link in my HTML file to be:

<link rel="stylesheet" type="text/css" href={{ STATIC_URL }}stylesheet.css />

where "stylesheet" is the name of the CSS file, and this file is located inside the directory listed under STATIC_DIRS in my settings.py file.  As mentioned in my prior email, I also already have STATIC_URL = '/static/' in my settings.py file, and I have the correct methods listed under STATICFILES_FINDERS.

But it is still not working.  Do I have a syntax error somewhere?  Or am I still missing something in another file?

thanks,
Guillaume

Kejun He

unread,
Aug 2, 2011, 5:15:46 AM8/2/11
to django...@googlegroups.com
hi,

Did you use the django development server to test it??

I have ever published my project on Lighttpd, but lost all the css.
I solved this problem by specified the midia and site-midia.

Like below on lighttpd.conf

        "/site_media/" => "/root/web/Affair/gmadmin/media/",
        "/media/" => "/root/web/Affair/gmadmin/media/"

AND


may be you could add a point before "/", like this

STATIC_PATH='./media'

Hope it could give you some help.

regards,
kejun

Thomas Orozco

unread,
Aug 2, 2011, 5:56:56 AM8/2/11
to django...@googlegroups.com

Could you show us the following:

Your staticfiles settings.
The link where your browser fetches the css file (see rendered source head when browsing) to gets 404'ed.

If you are using a local server, you could try using a STATIC URL of 127.0.0.1:8000/static/.
Note that this will not work if you use runserver on 0.0.0.0, on which case you should use your local IP address.

On a production server you can do the same, just use your server address.

Finally, you probably did but, have you activated staticfiles in your apps ?

Off Topic: media and static are supposed to be two different things, static is for your actual static files, and media for user-uploaded files, both work  pretty much the same way, so it's not a fix to use one in place of the other.

Thomas Orozco

unread,
Aug 2, 2011, 6:01:06 AM8/2/11
to django...@googlegroups.com

A little detail, but you never know: you should put some ' ' in your link.
Like this :

<link rel="stylesheet" type="text/css" href='{{ STATIC_
URL }}stylesheet.css' />


If there are spaces in your path, this could cause the lookup to fail.

And even if that's not the case, don't rely on the user's browser to fix coding mistakes.

Le 2 août 2011 10:28, "Guillaume Chorn" <guillau...@gmail.com> a écrit :

Thomas Orozco

unread,
Aug 2, 2011, 6:06:50 AM8/2/11
to django...@googlegroups.com

Last thing, I made a typo, the address you could use should start with http, like: http://127.0.0.1/static/

lokesh

unread,
Aug 2, 2011, 10:01:53 AM8/2/11
to Django users
Try this way which works well.
create folder static_media in project.
In urls.py give below url
(r'^static_media/(?P<path>.*)','django.views.static.serve',
{'document_root':'static_media'}),
in settings give,
MEDIA_URL = '/static_media/'
it should work.

Guillaume Chorn

unread,
Aug 3, 2011, 12:20:37 AM8/3/11
to django...@googlegroups.com
Ok, so I did use quotes in my link, and I changed my STATIC_URL setting (in settings.py) from '/static/' to 'http://127.0.0.1:8000/static/' (I am currently using the built-in development server at this address), but still no luck.

I'm not sure exactly what you mean by my "staticfiles settings."  (Sorry, once again I'm a total beginner so please bear with me.)  My current settings related to static files in 'settings.py' are as follows:

STATIC_ROOT = ''
STATIC_URL = 'http://127.0.0.1:8000/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = ('/Users/guillaumechorn/Documents/website/mainapp/static')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)

Below are the full contents of my urls.py file:

from django.conf.urls.defaults import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^home/$','mainapp.views.homepage'),
    (r'^mainapp/$','mainapp.views.index'),
    (r'^mainapp/(?P<company_id>\d+)/$', 'mainapp.views.detail'),
    (r'^products/$','mainapp.views.productindex'),
    (r'^products/(?P<product_id>\d+)/$','mainapp.views.productdetail'),
    (r'^news/$','mainapp.views.news'),
    (r'^events/(?P<event_id>\d+)/$','mainapp.views.eventdetail'),
    (r'^partners/$','mainapp.views.partners'),
    (r'^admin/', include(admin.site.urls)),
)
urlpatterns += staticfiles_urlpatterns()

Once again, I'm using the following to link to my CSS file:

<link rel="stylesheet" type="text/css" href='{{ STATIC_URL }}stylesheet.css' />

Am I still missing something?  Did I enter in anything incorrectly?

thanks,
Guillaume

Guillaume Chorn

unread,
Aug 4, 2011, 11:52:28 AM8/4/11
to django...@googlegroups.com
Hi All,

It's been a couple of days since I last wrote and no response so I'm trying again (see below).  Please help if you can; without being able to link to external static files I really can't do anything else with my site.  CSS is the very least of my concerns--worst case I can include that info in each HTML file--but I need to be able to use some basic javascript for my site and right now, because of this issue, I can't even do that.  Is there anyone who could help me troubleshoot this?  Anyone??

thanks,
Guillaume

Amao Zhao

unread,
Aug 4, 2011, 11:59:23 AM8/4/11
to django...@googlegroups.com
Hi, you should conform the django.contrib.staticfiles in your INSTALLED_APPS, then add the prefix to your template:{% load static %},

Stuart MacKay

unread,
Aug 4, 2011, 12:19:50 PM8/4/11
to django...@googlegroups.com
Guillaume,

Here are some settings that you should compare to see if your project is set up correctly:

In settings.py

# define the root directory to your project - this is just a convenience so everything is referenced correctly.
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))

# the directory for your static files will be a sub-directory in your project
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'

 # this allows you to use {{ STATIC_URL}} in your templates so you don't have to hardwire the path
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.static',
    ....
)

# Django searches all the installed apps for ./static sub-directories so make sure you include your own
# application so the files can be found - this is the most common mistake I make.
INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    ...
    'my-app'
)

Finally in urls.py make sure django is serving up the static files correctly - this is only for your local devleopment machine so you can find out if everything is working (for production you should configure your web server to serve up the files):

if settings.DEBUG:
    urlpatterns += patterns('django.views.static',
        (r'^%s(?P<path>.*)$' % settings.STATIC_URL[1:] , 'serve',
         {'document_root': settings.STATIC_ROOT}),
    ...
    )

This should get everything working please let me know if it does not.

Regards,

Stuart MacKay
Lisbon, Portugal

vev...@yandex.ru

unread,
Aug 5, 2011, 11:30:18 PM8/5/11
to Django users
Hello,
I can't use static files too.
urls:
urlpatterns = patterns('',
(r'^', 'testjquery.views.first'),
)
if settings.DEBUG:
urlpatterns += patterns('django.views.static',
(r'^%s/(?P<path>.*)$' % settings.STATIC_URL[1:], 'serve',
{ 'document_root': settings.STATIC_ROOT }),
)

settings:
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)),
*x)
DEBUG = True
TEMPLATE_DEBUG = DEBUG
STATICFILES_DIRS = DEBUG and (rel('static'),) or None
STATIC_ROOT = rel('static')
STATIC_URL='/static/'
TEMPLATE_CONTEXT_PROCESSORS =
('django.core.context_processors.static',)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
{% load static %}
</head>
<body>
<div id="logotype">
<img src="{{ STATIC_URL }}images/ob_1_1.png">
</div>

Vladimir
Moscow, Russia

Ванин Владимир Евгеньевич

unread,
Aug 5, 2011, 11:38:05 PM8/5/11
to django...@googlegroups.com
Hello,
I tried click. Error 404.
Vladimir

06.08.2011, 07:31, django...@googlegroups.com:
> I am out of office right now and will get back to you when I return. If you don't hear from me, my assistant should contact you shortly. In the meantime, you should take a look at this product I have been using..Click Here
>
> Enable images or click here
>
> Let me know what you think after you have a chance to review.
> Cheers!
>
> If you no longer wish to receive emails, please unsubscribe
>
> 866-288-1880
> 5150 yarmouth ave, Encino, CA 91316
> On Fri, 5 Aug 2011 20:30:18 -0700 (PDT), "vev...@yandex.ru" wrote:

--
О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫, О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫

Reply all
Reply to author
Forward
0 new messages