0

I used cPanel and deployed a Django application on my server using passenger_wsgi.py. The problem is when I'm trying to access static files (like admin CSS file: static/admin/css/base.css) I'm facing with 404 error. I've already done collectstatic and added PassengerPathInfoFix method to passenger_wsgi.py file but the output log is

Not Found: /home/mysite/public_html/build/static/admin/css/base.css

even though the outputted path exists and I can edit it using vim.

My settings.py:

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Any help would be appreciated.

Gtslix
  • 153
  • 1
  • 7
  • Does this answer your question? [Django runserver not serving static files in development](https://stackoverflow.com/questions/20398600/django-runserver-not-serving-static-files-in-development) – Ivan Starostin Jul 14 '22 at 11:41
  • @IvanStarostin No, I've copied "static" to the base directory of my domain and it works. Thanks for your comment – Gtslix Jul 14 '22 at 12:44

4 Answers4

0

Add this into url.py

from django.conf.urls.static import static

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Hope this help!

MVB
  • 1
  • 3
0

You can try to change STATIC_URL to STATICFILES_DIRS, this work with me!

STATICFILES_DIRS = [
    BASE_DIR / 'static',
    BASE_DIR / 'static/admin',
]
MVB
  • 1
  • 3
0

Thanks to Ali's answer, For anyone with the same problem, here are the steps I've done. Wish it would be helpful:

  1. Make sure your settings.py file contains these lines:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
  1. run python manage.py collectstatic

  2. Edit your passenger_wsgi.py and add these lines:

# ...
# Import WSGI of your project

# Project Static File Path
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/myapp')
SCRIPT_NAME = os.getcwd()
class PassengerPathInfoFix(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        from urllib.parse import unquote
        environ['SCRIPT_NAME'] = SCRIPT_NAME
        request_uri = unquote(environ['REQUEST_URI'])
        script_name = unquote(environ.get('SCRIPT_NAME', ''))
        offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
        environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
        return self.app(environ, start_response)

application = PassengerPathInfoFix(application)
  1. If you deployed your Django project in a subfolder inside public_html and not in the public_html directory (as mine), copy the static folder to public_html or create symbolic link using ln -s public_html/static/ public_html/subfolder/static/ (The point is the static files only load from your domain/subdomain base directory) (maybe there is a better solution but this solved my problem)

So I moved the project to the base directory of my domain and it worked.

Gtslix
  • 153
  • 1
  • 7
0

install whitenoise and that's it !

  1. pip install whitenoise

  2. settings.py :

    MIDDLEWARE = [
        # ...
        "django.middleware.security.SecurityMiddleware",
        "whitenoise.middleware.WhiteNoiseMiddleware",
        # ...
    ]
    
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Uralbi
  • 37
  • 2