1
votes

I tried fetching static files for my website but nothing seems to work even the other stackoverflow answers. please help on this.

settings.py -

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR,'static','static_root')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static','static_dirs')
]    

File is present in : parent_folder>static>static_dirs>css>cover.css

HTML

<html lang="en">
{% load staticfiles %}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Aman Turate">
<title>Aman Turate - Resume</title>
<link rel="stylesheet" href="{% static 'css/cover.css' %}">
2
which Django version are you using? - Giordano
@Giordano its 2.1.4... - Aman Turate

2 Answers

0
votes

This works for me. Update your settings.py to this

.........
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
.........

Make sure the static folder is in the root (i.e where manage.py is)

0
votes

There are several things to consider here and I am not sure which applies because I'm working on pure assumptions of your situation without knowing:

  1. What is BASE_DIR set to
  2. Have you run manage.py collectstatic
  3. what is your server setup or are you just working off the django development server?
  4. Where are the files you're trying to link to actually placed

Anyway here is some info I hope will be useful. I will break down how the settings files relates to your template file and hopefully that will help you debug your problem.

STATIC_URL = '/static/' -- > the value of this is what will be appended to the static file you are linking in your template. It is the relative url after your domain name. So {% static 'css/styles.css' %} will be rendered as /static/css/styles.css in your html page when it is loaded.

STATIC_ROOT is the absolute path of where your files are located on disk. It tells django where to place all static files collected from your apps when you run manage.py collectstatic see here for details.

STATICFILES_DIRS tells django where to find project static files. By default Django will look for a static directory in each registered app and collect the files in there and place then in your STATIC_ROOT folder. If you want to put static files in a different directory in your project and you want django to know about it then you list those paths in this config variable.

With your code:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR,'static','static_root')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static','static_dirs')
]    

{% static 'css/cover.css' %} is translating to /static/css/cover.css, your telling django the root directory to collect static files is <your BASE_DIR>/static/static_root so here you can see that there might be a miss match in locations. Again I don't know if you ran collectstatic. your STATICFILES_DIR is just going to look for static files in <your BASE_DIR>/static/static_root to then put them in <your BASE_DIR>/static/static_root ... if that makes sense.