4

By using gunicorn without nginx from digitalcloud tutorial my server is runing and on the console is

not found: /static/style.css

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

I already tried to

  1. collectstatic

  2. do urlpatterns += staticfiles_urlpatterns() in urls.py file

  3. makemigrations + migrate

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
BareTalk
  • 63
  • 1
  • 6

4 Answers4

0

maybe django try read 'static//static/style.css' but you need this: 'style.css' ;) ?

rad_the_hero
  • 3
  • 1
  • 4
0

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

urlpatterns = [your urls here] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Vijay
  • 1
  • 3
0

Did you try to update Nginx config for fixing this issue? if not working why don't you try Django package Whitenoise. It is easy to install and easy to use if you followed the instructions.

some simplified steps:

  1. Collect static - make sure static files existing

     python manage.py collectstatic
    
  2. Install the Whitenoise - this step depends how you managed the packages, update proper file(e.g. Pipfile or requirements.txt) and install. Below command just a example to install the package, might not suits your project.

     pip install whitenoise
    
  3. Add the following STATICFILES_STORAGE in settings.py - this will compress and cache the static files(from Whitenoise docs)

     STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    
  4. Add the following to your MIDDLEWARE in settings.py - from Whwitenoise docs, Whitenoise package should place after django.middleware.security.SecurityMiddleware

     `MIDDLEWARE = [
       'django.middleware.security.SecurityMiddleware',
       'whitenoise.middleware.WhiteNoiseMiddleware', #add it here exactly after security middleware
       ...
     ]
    
  5. now restart or rebuild the app to check whether it is working for you.

Please check Whitenoise docs if you running into issue about installing the Whitenoise

a message for using staticfiles_urlpatterns: this only works when DEBUG=True in settings.py which means you should NOT use it for production environment. see reference here

Wei Jing
  • 613
  • 5
  • 11
-1

You can create in your project folder settings then in them create settings files like names: local.py

from my_project.settings import *
DEBUG = True
ALLOWED_HOSTS = []
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

this run in VM like: python manage.py runserver --settings settings.local

remember in urls add:

if settings.DEBUG == True:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

web.py

from my_project.settings import *
DEBUG = False
ALLOWED_HOSTS = ["www.side.com", "side.com"]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

and then in gunicorn import only web.py file project to read in nginx

this run in VPS server.

rad_the_hero
  • 3
  • 1
  • 4