Django CSS Wide Format Files

I'm struggling to get css to work globally on my Django project.

I have a STATICFILES_DIRS called 'project_static' with a css file. This is at the root of my project. In my settings.py, I have:

STATICFILES_DIRS = ( '/Users/gavinhinfey/django_projects/ss_stream/project_static/', ) 

In base.html, I have:

 <link rel="stylesheet" href="{{STATIC_URL}}css/main.css"> {% block content %} {% endblock content %} 

The css file is perfectly linked when I am in the page template in the stream application that I created. However, when I look at a page template that is not specific to the application, it does not see css.

Any idea why this is?

I did my best to explain this, but if you need to clarify the problem, please ask.

Thanks Gavin

+4
source share
2 answers

{{STATIC_URL}} was deprecated (I think) so that it might only show css/main.css .

I suggest you configure it as follows:

settings.py

 import os.path PWD = os.path.dirname(os.path.realpath(__file__)) # project root path STATICFILES_DIRS = ( PWD + '/static/', # or project_static, whatever ) 

base.html

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

Thus, you can use relative paths in your settings and avoid breaking settings if you move the entire project outside of your home directory.

You can use it for each path parameter, for example LOCALE_PATHS or TEMPLATE_DIRS .

If this does not work, make sure that you have the following settings:

 STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) # optional but if you defined it be sure to have this one: TEMPLATE_CONTEXT_PROCESSORS = ( # ... 'django.core.context_processors.static', # ... ) 
+3
source

Note that STATICFILES_DIRS is interable with directories where you can find static files for the project. But the application should not search for files in one folder. It should look for files in STATIC_ROOT (this is different). Resource files will only be in STATIC_ROOT after manage.py collectstatic

0
source

Source: https://habr.com/ru/post/1496618/


All Articles