Report this

What is the reason for this report?

problem in serving static files in django webapp

Posted on December 18, 2020

The urls.py file:

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The settings.py file:

STATIC_URL = '/web/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = '/home/domain.com/public_html/web/public/static'

I am using cyberpanel with openlightspeed server with contex:

context / {
  type                    appserver
  location                /home/domain.com/public_html/web
  binPath                 /usr/local/lsws/fcgi-bin/lswsgi
  appType                 wsgi
  startupFile             web/wsgi.py
  envType                 1
  env                     LS_PYTHONBIN=/home/domain.com/public_html/bin/pyhton
  env                     PYTHONHOME=/home/domain.com/public_html/
}

My web app is not serving static files I am getting:

404 Not Found


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

Here’s a general approach you could try to diagnose and fix the issue:

  1. Check the Paths: Make sure that the paths in your settings.py are correct and accessible by the web server.

  2. Configure OpenLiteSpeed to Serve Static Files: You’ll need to add a context for serving static files in your OpenLiteSpeed configuration. This will tell OpenLiteSpeed where to find the static files and serve them directly without going through your Django app.

    You can add the following context to your OpenLiteSpeed configuration file or through the admin interface:

    context /web/static/ {
        type static
        location /home/domain.com/public_html/web/public/static
        allowBrowse 1
        rewrite  {
            enable 0
        }
        addDefaultCharset off
        expireInSeconds 604800
    }
    

    Make sure to adjust the paths as needed based on your directory structure.

  3. Collect Static Files: Run the collectstatic command to make sure all your static files are in the correct location:

    python manage.py collectstatic
    
  4. Check Permissions: Ensure that the permissions are set correctly on the static files directory so that OpenLiteSpeed can read them.

  5. Restart OpenLiteSpeed: Finally, you’ll need to restart OpenLiteSpeed to apply the changes:

    sudo /usr/local/lsws/bin/lswsctrl restart
    
  6. Verify URLs: Verify that the URLs to your static files in your HTML templates are being rendered with the correct path based on your STATIC_URL setting.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.