8

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?

1
  • have you found the solution? Commented Apr 1, 2019 at 13:39

7 Answers 7

18

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

3 Comments

Thanks Amit, when I try to run the command, it returns this error: raise ImproperlyConfigured("You're using the staticfiles app " django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app wi thout having set the STATIC_ROOT setting to a filesystem path.
To be clear: I'm copying another web I made before and it worked this way and I think I didn't set STATIC_ROOT setting because I can't find anything named STATIC_ROOT in the settings.py (in previous web).
The solution is quite simple then, you need to add a line in your settings.py for STATIC_ROOT. for example: ``` STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env", "static_root") ``` Note it needs to fit your app structure. this video can give you some good idea about it: youtu.be/8cupCFknL4Q?list=PLEsfXFp6DpzRcd-q4vR5qAgOZUuz8041S
2

An issue about this deploy is that once it is published via AWS EB, static files (like style.css) are not read and the page lacks on style. Documents and forums are not very clear on pointing out this is a Django way of deploying and the web server must be instructed to use these files are served.

Once STATIC_ROOT and STATIC_URL are defined inside project/settings.py and added to urlpatterns inside project/urls.py, the problem is solved.

STATIC_ROOT holds the path where from the static files will be served.
collectstatic will place app and admin files inside STATIC_ROOT.
STATIC_URL holds the link to STATIC_ROOT, when requested from the browser.
+ static(settings[...] tells the web server that STATIC_URL is to be used.

I have a full functioning project with EB deploy instructions here: https://github.com/nandoabreu/django-polls

Comments

1

In the directory that has the following code

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

This code might give issues because of the double quotation marks used mixing up the quote signs should fix this, use the following code instead:

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

Comments

0

You probably need to add your settings and static url in your "urls.py" like in the below.

#urls.py

from django.conf import settings
from django.conf.urls.static import static

You can also see the documentation of django in the link below. https://docs.djangoproject.com/en/3.2/howto/static-files/

Comments

0

I will go on a dumb answer here, and I know this is very late but it might help someone in the future, but did you escape the double quotes inside the string?

"{% static these ones --->"css/bootstrap.min.css" %}"

Comments

0
-> update your settings.py<br>
STATICFILES_DIRS = [ 
    BASE_DIR / 'static'
    ]

-> add the template tag in the base.html file to load the static files<br>
{% load static %}

-> link the css file using the templates<br>
link rel="stylesheet" href="{% static 'main.css' %}"

2 Comments

Don't just answer with code, try to explain your answer not just in the code comments.
@Daviid I have explained the codes in short 1 liners. I have very limited knowledge about Django as of now and I believe any further explanation would be redundant over official documentation. If user or you have any specific questions about the same, I would be happy to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.