Question

While deploying a Django app. I am having problem with static files. I am having JS and CSS files in static directory. But browser is not able to find those files. I am having same files and settings on local machine that is ubuntu same as server OS, and have working on local. Although it has same settings but I am pasting that static directory and files setting below.

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(ROOT_PATH,'static')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'


# Additional locations of static files
STATICFILES_DIRS =(
                os.path.join(ROOT_PATH,'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',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

Here root path in above code is defined before that is :

import os
#from django.contrib.sites.models import Site
ROOT_PATH = os.path.dirname(__file__)

I am using mod_wsgi with apache2, in my understanding it should find the files when it is loading /somefoldername then browser should go in that directory but didn't get where is problem. To know about environment and wsgi settings e.t.c. I am following here is question in which I posted my wsgi file details and way it is working: 500 server error on Django site after shifting to server

I am many time having issues while finding apps and other templates and modules etc. even in syncdb commmand on server, for templates I appended root path and it started working also added project path in wsgi file but still that static files issue is not solved.

Was it helpful?

Solution 2

OTHER TIPS

When you run "collectstatic", are all files copied to STATIC_ROOT? You should verify this first so you know the files are where they should be.

Have you configured apache to serve the static files in STATIC_ROOT as "/static/"? Django does not serve static files for you in production (DEBUG=False) so you need to configure your web server software to serve the files for you.

Also, I don't think you want your STATICFILES_DIRS to contain the same path as STATIC_ROOT. Files are copied from the dirs in STATICFILES_DIRS to STATIC_ROOT. Your STATICFILES_DIRS should contain dirs that are not otherwise searched during "collectstatic".

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.

If you still need to server static locally (e.g. for testing without debug) you can run dev server in insecure mode:

python manage.py runserver --insecure

Enjoy cheer :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top