Getting 404 for all static files during WSGI setup with django
Asked Answered
T

4

5

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.

Trover answered 16/10, 2015 at 10:32 Comment(6)
Have you run collectstatic?Ptolemaic
Yes, the directory is filled with all the imagesTrover
Moreover, i dont understand why it works before i started with uwsgi ?Trover
are you trying to serve static files with uwsgi itself or are you using any http server ?Cryo
No i would like django server to serve those files , till the time i install nginxTrover
afaik you should have debug=True to make staticfiles app work. I have not done http through uwsgi but it seems to support it uwsgi-docs.readthedocs.org/en/latest/HTTP.html . You could try that.Cryo
A
3

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)
Adur answered 16/10, 2015 at 10:54 Comment(5)
It says , name static is not definedTrover
see docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/… . This is the way to serve static in different development serversCryo
in urls import: from django.conf.urls.static import staticAdur
Superb, that worked. But wait i have one query about your answerTrover
I already had the two lines already added in my settings.py, but was missing the urls.py content. Can you tell me why that still worked when i ran "python manage.py runserver " and not in uwsgi --ini uwsgi.ini --http :8000Trover
L
5

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

Lakeishalakeland answered 8/8, 2017 at 3:7 Comment(3)
thanks for your answer. I guess the time I asked this question --check-static wasn't available. Please add a note to your answer mentioning the version of uwsgi this works with. This is available with 1.9.Trover
the version of my uwsgi is 2.0.15Lakeishalakeland
django dir or application static dir?Bovid
C
4

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
Cutch answered 7/2, 2018 at 17:29 Comment(1)
ding ding ding! This is the winner right here. Make sure to run the django collectstatic command before doing this. Set your django STATIC_ROOT setting to /path/to/staticfiles for this post's examplePinebrook
A
3

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)
Adur answered 16/10, 2015 at 10:54 Comment(5)
It says , name static is not definedTrover
see docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/… . This is the way to serve static in different development serversCryo
in urls import: from django.conf.urls.static import staticAdur
Superb, that worked. But wait i have one query about your answerTrover
I already had the two lines already added in my settings.py, but was missing the urls.py content. Can you tell me why that still worked when i ran "python manage.py runserver " and not in uwsgi --ini uwsgi.ini --http :8000Trover
H
0

I don't know why, but it starts work when I replace this code:

if settings.DEBUG:
    urlpatterns += [
        url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
    ]

with this:

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

in file urls.py

I have Django 1.8

Homing answered 26/2, 2019 at 3:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.