João Dias
João Dias

Reputation: 135

Why do static files won't load in the django web app?

So I've seen a lot of tutorials here explaining what to do, but no matter what I change the static files won't seem to load. This is what my files are looking like atm

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(PROJECT_ROOT, "static"),'']

job_app/urls.py

urlpatterns = [
    path('', main, name='index'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JobPortal - Free Bootstrap 4 Template by Colorlib</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    {% load static %}

    <link href="https://fonts.googleapis.com/css?family=Nunito+Sans:200,300,400,600,700,800,900" rel="stylesheet">

    <link rel="stylesheet" href="{% static 'css/open-iconic-bootstrap.min.css' %}">
    <link rel="stylesheet" href="{% static 'css/animate.css' %}">

    <link rel="stylesheet" href="{% static 'css/owl.carousel.min.css' %}">
    <link rel="stylesheet" href="{% static 'css/owl.theme.default.min.css' %}">
    <link rel="stylesheet" href="{% static 'css/magnific-popup.css' %}">

    <link rel="stylesheet" href="{% static 'css/aos.css' %}">

    <link rel="stylesheet" href="{% static 'css/ionicons.min.css' %}">

    <link rel="stylesheet" href="{% static 'css/bootstrap-datepicker.css' %}">
    <link rel="stylesheet" href="{% static 'css/jquery.timepicker.css' %}">


    <link rel="stylesheet" href="{% static 'css/flaticon.css' %}">
    <link rel="stylesheet" href="{% static 'css/icomoon.css' %}">
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
  </head>
  <body>

jobber/urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('job_app.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Upvotes: 1

Views: 390

Answers (1)

Karmveer Singh
Karmveer Singh

Reputation: 953

Below configs worked for me. Django only needs two things.

  1. What should be the base static URL (STATIC_URL)
  2. And where to search static files (STATICFILES_DIRS)

Remove static urlpatterns from urls.py. Only define STATIC_URL and STATICFILES_DIRS in settings.py and remove STATIC_ROOT.

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

Upvotes: 2

Related Questions