0

My settings description.

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

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Also, I had added static files to urls.py urlpatterns += staticfiles_urlpatterns()

My HTML template

{% load static %}
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>Things I eat</title>
</head>
<body>
    <h1> Hello world</h1>
</body>

findstatic command output:

> python manage.py findstatic styles.css
Found 'styles.css' here:
  /project_name/static/css/styles.css

I use django == 3.0.7

I followed advice from similar questions [1,2,3] But my static files are not fetching while rendering. Any advice for debugging?

EDIT: My static dir structure:

static
-- css
---- styles.css
Daniel Chepenko
  • 2,229
  • 7
  • 30
  • 56

3 Answers3

1

I was facing same problems and tried many solution but at the end for me below approach worked, I hope it will also work for you

first of all no need to add STATICFILES_DIRS and STATIC_ROOT

settings.py

DEBUG = True

//make sure that django.contrib.staticfiles is present in your installed apps

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

//add this line at the end
STATIC_URL = '/static/'

and seems like you have load static but not added any css file, for that add below line in your html page header

<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
Mayur Satav
  • 985
  • 2
  • 12
  • 32
  • Thanks! Yeah, I didn't add any CSS file to the page header. – Daniel Chepenko Jun 19 '20 at 13:43
  • But still seems to face some problems with the path to my css. "GET /static/css/styles.css HTTP/1.1" 404 1782 – Daniel Chepenko Jun 19 '20 at 13:43
  • btw, why STATICFILES_DIRS and STATIC_ROOT are obsolete? – Daniel Chepenko Jun 19 '20 at 13:44
  • Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files. reference - https://docs.djangoproject.com/en/3.0/howto/static-files/ – Mayur Satav Jun 19 '20 at 14:15
  • have you refer this answer https://stackoverflow.com/a/24200094/8379852 have a look. if still not work then provide the screenshot of your server terminal – Mayur Satav Jun 19 '20 at 14:23
0

Did you include stylesheet link? <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css ' %}">

pritam samanta
  • 434
  • 4
  • 10
0

I always give the directory separately and then add the CSS folder later.

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

and then in html file:

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

For more info. https://docs.djangoproject.com/en/3.0/howto/static-files/

Aadesh Dhakal
  • 412
  • 6
  • 17