my django project static files can not be load with option debug=false

Question:

It is very strange that my django website (setup on a linux server with django 1.3) can be visit correctly with DEBUG = True. But when I changed the option to DEBUG = False the static content won’t load (css files and images can not be found)

Here’s related options i got in my setting.py:

DEBUG = False
STATIC_ROOT = ''
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = ("/home/ubuntu/ls_website/static/",)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Anybody have any idea? If the version is too low, I would wait a while for 1.5 release.

Asked By: Leon

||

Answers:

I also move between Windows and Linux for development. To get around the problem of absolute paths in settings.py, I use this function:

def map_path(directory_name):
    return os.path.join(os.path.dirname(__file__),
        '../' + directory_name).replace('\', '/')

Which you can implement in this fashion:

STATICFILES_DIRS = (
    map_path('static'),
)

This function is tailored for Django 1.4.x. You’ll need to modify it slightly for Django 1.3.x. Hope that helps you out.

Answered By: Brandon Taylor

I found when debug=False, django won’t load static files automatically. Options are set static path in urls.py or serve static files with apache.
People said set static path in urls.py is slower than serve static files with apache. Don’t know why though…

Answered By: Leon

Staticfiles doesn’t do anything when DEBUG=False. You need to serve those files with apache. Staticfiles has the ability to collect the files to one spot for you to make it easy, then you use some apache magic (assuming here since you didn’t specify) to have apache intercept those requests and serve the static files for you.

    Alias /robots.txt  /home/username/Python/project/site_media/static/robots.txt
    Alias /favicon.ico /home/username/Python/project/site_media/static/favicon.ico
    Alias /static/     /home/username/Python/project/site_media/static/

I don’t remember if it is buildstatic, build_static, collectstatic or collect_static to copy those files from your development stop to your deployment spot, but these variables control how staticfiles does its magic

# Absolute path to the directory that holds static files like app media.
# Example: "/home/media/media.lawrence.com/apps/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")

# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/static/"

# Additional directories which hold static files
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "static"),
    os.path.join(PROJECT_ROOT, "media"),
]

This assumes your static files are in the static folder of your project, and you want to serve them from the site_media folder.

Answered By: boatcoder
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.