Search code examples
pythondjangowsgiuwsgi

Getting 404 for all static files during WSGI setup with django


I have the following directory structure

-----root
   |___docpd
      |__docpd (contains settings.py, wsgi.py , uwsgi.ini)
      |__static

During my vanilla django setup in dev environment , everything was fine (all static files used to load). But now after setting up uwsgi, i found that none of my static files are being loaded(I get a 404 error).

What have i tried ?

1. Read alot of stack overflow questions about this error and none could solve my problem.

2. I adjusted the python path with paths to my project and they seem to get added as i printed them out in the settings.py file, but still no luck.

Some code

uwsgi.ini

    [uwsgi]
chdir=/home/ubuntu/docpad/Docpad/
wsgi-file=/home/ubuntu/docpad/Docpad/Docpad/wsgi.py
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/docpad.log

wsgi.py

    import os,sys
sys.path.append('/home/ubuntu/docpad/Docpad/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Docpad.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

settings.py

    from sys import path
from os.path import abspath, dirname, join, normpath

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
print "Base dir = " , BASE_DIR

DJANGO_ROOT = dirname(abspath(__file__))
print "Root =",DJANGO_ROOT

SITE_ROOT = dirname(DJANGO_ROOT)
print "SITE =", SITE_ROOT

path.append(DJANGO_ROOT)
print "Path = ",path

print BASE_DIR
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'SECRET'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

PROJECT_PATH = os.path.join(BASE_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)

TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'Docpad/templates/')
print "TEMPLATES = ",TEMPLATE_PATH

# TODO make separate template_paths
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    TEMPLATE_PATH,
)

...

After i run my application, i get 404 for all static resources like css/js files.

UPDATE

When i do

python manage.py runserver 0.0.0.0:8000

the server starts serving the static files

But , this command

uwsgi --ini uwsgi.ini --http :8000

gives me the problem(not loading the static files).

I am totally clueless right now,have been trying various alternatives but no luck.

If anybody could help, it'll be great.


Solution

  • in settings

    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = (
        os.path.normpath(os.path.join(BASE_DIR, "static")),
    )
    

    in urls.py

        urlpatterns = [
    #urls
    ]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)