Home > Blockchain >  How to make static directory in Django
How to make static directory in Django

Time:04-15

I created static directory which included css and image files in my Django project. When I run my project the css do display("GET /static/website/barber.css HTTP/1.1" 200 69) but the images do not("GET /static/website/images/balloons.gif HTTP/1.1" 404 1840),

CodePudding user response:

in your setting.py file define STATIC_URL

STATICFILES_DIRS=[
    os.path.join(BASE_DIR,'static'),
    os.path.join(BASE_DIR,'boot'),
]
STATTC_URL = '/static/'

and add this to your urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    #your url
]   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

CodePudding user response:

Add STATIC settings

You need to create these definitions in settings.py to tell Django where is static files:

STATIC_DIR = os.path.join (BASE_DIR, "static")
STATIC_ROOT = os.path.join (BASE_DIR,'static files')

STATIC_URL ='/static/'# path to read css with local (probably)
STATICFILES_DIRS = [
        os.path.join (BASE_DIR, "static"),
    ]

Be sure to import settings and static in urls.py.

You also need to add them to your main urls.py to access the URLs of images:

from django.conf import settings
from django.conf.urls.static import static  

urlpatterns = [ 
    #Your urls
]

if settings.DEBUG:
    urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Related