1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hafiz
  • 4,187
  • 12
  • 58
  • 111
  • where are the static files stored on the apache server? `MEDIA_ROOT` and `MEDIA_URL` should point to it. – sampson-chen Jan 01 '13 at 00:09
  • 2
    It sounds like you need to set an alias from `/static/` to your static directory in your apache sites config. – drewman Jan 01 '13 at 00:36
  • @drewman how can I set alias to /static/ in my apache sites config? – Hafiz Jan 01 '13 at 20:33
  • @drewman for MEDIA_ROOT and MEDIA_URL should I point them to `/static/` or to whole physical path of static files on server? – Hafiz Jan 01 '13 at 20:35
  • 1
    I'm not an expert in apache configs. I would recommend taking a look at the [docs](http://httpd.apache.org/docs/). – drewman Jan 01 '13 at 21:27
  • 1
    `MEDIA_ROOT` and `MEDIA_URL` are used for serving files uploaded to your database. I would recommend setting `MEDIA_ROOT = path.join(TOP_DIR, 'mediafiles')` and `MEDIA_URL = '/media/'`. You will also need to have apache serve those files. – drewman Jan 01 '13 at 21:29
  • 2
    The Django documentation tells you how to set up static files when using mod_wsgi. See https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#serving-files – Graham Dumpleton Jan 02 '13 at 01:43
  • @drewman thanks your comments were helpful for me and Graham's comment solved my problem. – Hafiz Jan 02 '13 at 10:18
  • @GrahamDumpleton please put your comment as answer so that I can mark it as correct and others can also see that as correct answer. – Hafiz Jan 02 '13 at 10:18

3 Answers3

2

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

Daniel Eriksson
  • 3,814
  • 3
  • 16
  • 11
  • I haven't run collectstatic , just uploaded files from my machine to server and are in static directory. – Hafiz Jan 01 '13 at 20:19
  • In general, you should set `STATIC_ROOT` set to a different directory than anything listed in `STATIC_DIRS`. I recommend `STATIC_ROOT = path.join(TOP_DIR, 'staticfiles')`. – drewman Jan 01 '13 at 21:29
  • If you don't use "collectstatic" then STATICFILES_DIRS is never used and should be empty. Since you manually upload the static files you know they are in the right place. The next step is to configure your web server software (Apache in your case) to serve the files located in your STATIC_ROOT when http://yourdomain/static/ is requested. – Daniel Eriksson Jan 02 '13 at 00:03
  • Again, Django will not automatically serve static files for you when configured for production. It is possible to serve static files in production using Django, but please don't because it is a resource hog. Read more about serving static files here: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production – Daniel Eriksson Jan 02 '13 at 00:12
0

The Django documentation covers the topic. See:

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
0

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 :)

Ashish Gupta
  • 1,153
  • 12
  • 14