Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 996
cafebabe1991 Avatar asked Dec 19 '22 23:12

cafebabe1991


2 Answers

I solved problem by doing below:

add

check-static = /your django dir/

in uwsgi.ini file. it works for me!

Suppose your static assets are under /customers/foobar/app001/public. You want to check each request has a corresponding file in that directory before passing the request to your dynamic app. The --check-static option is for you: --check-static /customers/foobar/app001/public

http://uwsgi-docs.readthedocs.io/en/latest/StaticFiles.html

like image 58
DennisLi Avatar answered Dec 24 '22 02:12

DennisLi


You could also do it like so:

uwsgi --ini uwsgi.ini --http :8000 --static-map /static=/path/to/staticfiles/

or just add this to your .ini file:

static-map = /static=/path/to/staticfiles
like image 42
Humphrey Butau Avatar answered Dec 24 '22 03:12

Humphrey Butau