Django dev server intermittently fails to serve static files

870 views Asked by At

In my development environment I'm getting intermittent failures for serving static files (js scripts and css). In the error console in Chrome I get 404s. But if I refresh on those items, or visit the URLs directly, they're served up fine.

This is annoying.

Example:

GET http://127.0.0.1:8000/static/js/editor/xyz.js?v=1 404 (NOT FOUND)

but if I visit that URL directly fine. And if I refresh the page a few times, it will work again.

Any ideas?

Chrome 14.0.835.202

Django==1.3
Fabric==1.0.1
Jinja2==2.5.5
PIL==1.1.7
Pygments==1.3.1
South==0.7.3
Sphinx==1.0.5
boto==2.0
chunks==0.1
django-devserver==0.2.1
django-pagination==1.0.7
django-sorting==0.1
django-storages==1.1.3
docutils==0.8
gunicorn==0.12.1
ipython==0.10.1
paramiko==1.7.6
pep8==0.6.1
psycopg2==2.2.2
pycrypto==2.0.1
python-dateutil==1.5
python-memcached==1.45
wsgiref==0.1.2
3

There are 3 answers

3
jujule On

The dev server is single-threaded so if something keeps waiting, it blocks every request.

I usualy work with the django concurent dev server which is multi-threaded and works much better. Also it is very fast and easy to setup ;)

2
EsseTi On

it might depends on your setup. what did you do for the static? what's the settings? did you do the collect static? try this in case

however, about serving static files in development:

Warning This will only work if DEBUG is True.

That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.

from here

can't you just supply the static files in another server?

3
Manuj Rastogi On

After reading all answers if still anyone having this problem then ... As per Django nature you don't need to do anything to serve static files just your settings file should have proper configuration as follows:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    # **THIS IS USED WHEN YOUR STATIC FILES ARE IN SOME OTHER FOLDER ALSO**

    # 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.

    FOLDER_NAME, 
)

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

INSTALLED_APPS = (
    # other apps
    'django.contrib.staticfiles',
)

But if still you face problem put this into your urls.py:

(r'^(path of your file)$', 'django.views.static.serve' , {'document_root': 'PROJECT_ROOT_DIR' + "path to the static folder"}),

Above URL will serve for static files whether they are JS files or CSS or images.

In case of production server you don't need this.

Then run: python manage.py collecstatic.

Hope this helps.