I'm trying to add a background picture to the hero section in Django, But whenever I open that URL: http://127.0.0.1:8000/static/img/bg.png
it says 'img\bg.png' could not be found
I also attempted to open other urls, but they are broken.
#settings
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = 'E:\coding\django\pawnhost\static'
#urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("core.urls")),
] static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
#html
<section >
...
</section>
#CSS (base.html)
{% load static %}
<style>
.Hero {
background-image: url({% static 'img/bg.png' %});
}
</style>
CodePudding user response:
url:
static(settings.STATIC_URL, document_root=settings.STATIC_DIR)
settings:
STATIC_DIR = BASE_DIR / 'static'
STATIC_ROOT = BASE_DIR/"static_root"
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
try to use STATIC_DIR & STATIC_ROOT different name for confusion.