Azima
Azima

Reputation: 4151

django static files are not being found and loaded

Using django v2.2.1

I have a given project dir. structure:

enter image description here

In root project dir. there are some static files in static dir.

In base.html file,

{% load static %}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- favicon --> 
    <link rel="shortcut icon" href="{% static 'favicon.ico' %}" />    

    <!-- Dropzone js -->
    <link rel="stylesheet" href="{% static 'dropzone.css' %}">
    <script src="{% static 'dropzone.js' %}" defer></script>

    <!-- Custom js & css -->
    <link rel="stylesheet" href="{% static 'style.css' %}">
    .
    . 

This is settings.py file:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIR = [
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'sales', 'static'),
]

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

In urls.py

from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls), 
    path('', include('sales.urls', namespace='sales')),   
    path('reports/', include('reports.urls', namespace='reports'))   
] 

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Also urlpatterns for sales

urlpatterns = [
    path('', home_view, name='home'),
    path('sales/', SaleListView.as_view(), name='list'),
    path('sales/<pk>/', SaleDetailView.as_view(), name='detail'),
]

Tried

$ python manage.py findstatic style.css
No matching file found for 'style.css'.

Also I've been getting this error

enter image description here

I have restarted the server many times.

Upvotes: 1

Views: 448

Answers (1)

Bishwo Adhikari
Bishwo Adhikari

Reputation: 93

Adding the following to TEMPLATES in settings.py should help in your case:

  • For Django3.x
TEMPLATES = [
 {
    ...
    'DIRS': [BASE_DIR / 'templates'],
    'APP_DIRS': True,
    ...
}
  • For Django2.x
import os
...
TEMPLATES = [
 {
    ...
    'DIRS': os.path.join(BASE_DIR, 'templates'),
    'APP_DIRS': True,
    ...
}

Upvotes: 1

Related Questions