[Answered ]-My django project static files can not be load with option debug=false

1👍

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…

👤Leon

1👍

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.

0👍

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.

Leave a comment