Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django can't find css (static folder)

I'm trying to find a way, how to tell the Django where static files like css are stored. The problem is that it can't find the static folder probably. I've set the settings.py file already but it did not help.

SETTINGS.PY:

STATIC_URL = '/static/'

INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
)

BASE.HTML:

...
...

<title>Rozcestník</title>

<!-- Bootstrap core CSS -->
  {% load static %}
  <link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet">
...

PROJECT STRUCTURE: enter image description here

Where is the problem?

like image 732
Milano Avatar asked Dec 01 '22 13:12

Milano


2 Answers

I don't like much you directory (structure).

If the folder "web" is an app and "dedo" is a folder with settings, than you cannot have folders "static" and "templates" inside your app folder.

It shoud be:

dedo
  __init__.py
  settings.py
  urls.py
  views.py
static
  css
    bootstrap.min.css
templates
  base.html
  index.html
web
  migrations
  __init__.py
  admin.py
  models.py
  tests.py
  views.py
db.sqlite3
manage.py

And in your setttings.py you need to write this:

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

STATIC_URL = access via browser to your static files

STATICFILES_DIRS = where Django will look up static files

IMPORTANT:

STATIC_ROOT = is used ONLY if you are going to deploy your web app via appropriate web server (nginx, apache, ...) with WSGI (gunicorn, modwsgi, ...)

"python manage.py collectstatic" or "python3.X manage.py collectstatic" are commands for copying your static files from STATICFILES_DIRS (plus static files for /admin/) to STATIC_ROOT, which means that STATIC_ROOT mustn't be the same as STATICFILES_DIRS.

like image 60
Yaaaaaaaaaaay Avatar answered Dec 06 '22 08:12

Yaaaaaaaaaaay


I guess you didn't use the "Collect static" action.

In your shell/CMD run: python manage.py collectstatic


If it didn't solve your problem post the error you're getting.

like image 44
Amit be Avatar answered Dec 06 '22 08:12

Amit be