Django collectstatic does not collect media files from react npm build folder - django

I have a frontend react app, after using npm run build it creates build folder with:
build
favicon.ico
index.html
service-woker.js
static
After using django's python manage.py collectstatic I noticed what django has done was that it pulls out only the static folder, favicon.ico is not pulled out. So, my website icon doesn't work.
In my index.html, <link rel="apple-touch-icon" href="%PUBLIC_URL%/favicon.ico" />
In my settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../frontend/build/static')
]
STATIC_ROOT = '/var/www/web/home/static/'
STATIC_URL = 'home/static/'
In chrome inspect in the headers element:
<link rel="icon" href="./home/favicon.ico">
How do I get it to display my web icon. Thankyou!

It is clear in documentation that Django collectstatic looks only for files in folders that are set in
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../frontend/build/static')
]
This will copy all files from your static folders into the STATIC_ROOT
directory.
your favicon is not in any of listed staticfiles directiories
Second thing is that Django static files are only accessible from full STATIC_URL path ( you cannot use just .home/ path)
Fix would be one of following
to simply add icon inside static folder
use ngnix to serve static files and add proper blocks ( prefered )
change STATIC_ROOT='/var/www/web/home/' and STATIC_URL = 'home/' ( note this way index.html and rest of files in home would be accessible as staticfiles)

Related

Django Website does not load static files

I am in the middle of creating a django project and I have templates and static folders within the project directory. I can render and view the html files however it cannot load css files stored in the static folder. I have placed the load static tag in my html file but when I run python manage.py runserver it I get this error
Performing system checks...
Watching for file changes with StatReloader
System check identified some issues:
WARNINGS:
?: (staticfiles.W004) The directory '/static' in the STATICFILES_DIRS setting does not exist.
System check identified 1 issue (0 silenced).
December 08, 2022 - 14:54:53
Django version 4.1.3, using settings 'brighterstrat.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
This is how I have referenced the static files in my html file
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css'%}">
<link rel="stylesheet" href="{% static 'css/style.css'%}">
setting.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, '/static')]
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
How can I make it load the css files
Django statics can be confusing, mostly because the behavior is different between prod and development (i.e DEBUG=True)
To recap the settings:
# Url at which static files are served
# It's the url the browser will fetch to get the static files
# It's prepend to static name by the {% static %} templatetag
STATIC_URL = "static/"
# Directory where static files can be found
# When DEBUG = True, static files will be directly served from there by
# the manage.py runserver command
STATICFILES_DIRS = [BASE_DIR / "static"]
# Directory to export staticfiles for production
# All files from all STATICFILES_DIRS will be copied by
# manage.py collectstatic to this directory.
# /!\ It will not be served by django, you have to setup
# your webserver (or use a third party module)
# to serve assets from there.
STATIC_ROOT = BASE_DIR / "assets"
In you example, it seems you didn't place your files in a directory listed by STATICFILES_DIRS so make sure you have those files:
| manage.py (root of your django app)
| static
| |- css
| | |- bootstrap.min.css
| | |- style.css
To debug this, you can add a print in your settings:
print([os.path.join(BASE_DIR, '/static')])
To fix this, you just have to remove the slash for it to work correctly.
From the os.path.join() docs:
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
That's why BASE_DIR is being cut off from your STATICFILES_DIRS variable.

How to server favicon.ico with Django and Whitenoise

I use whitenoise for static files and it works fine.
But how can I serve the /favicon.ico file?
There is a setting called WHITENOISE_ROOT, but I don't understand how to use it.
I would like to keep my nginx config simple and serve all files via gunicorn
If you want those files to be managed by collectstatic
Let's assume after running collectstatic, your favicon.ico file ends up being copied in a root subdirectory, located in your STATIC_ROOT directory.
Then, with:
WHITENOISE_ROOT = os.path.join(STATIC_ROOT, 'root')
Whitenoise will serve all files in STATIC_ROOT/root/ at the root of your application.
In your case, STATIC_ROOT/root/favicon.ico will be served at /favicon.ico.
If you don't want those files to be managed by collectstatic
You can have a root_staticfiles folder in your BASE_DIR which simply contains the static files you want to serve at /.
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'root_staticfiles')
In this case, Whitenoise will serve all files in BASE_DIR/root_staticfiles/ at the root of your application.
Update about pathlib (2022-10-04)
Since a while now, the default settings.py Django creates uses pathlib. To be consistent with it, one may replace os.join calls with / operators, eg.:
WHITENOISE_ROOT = STATIC_ROOT / 'root'
You could as per this answer by hanleyhansen add the following line in a base template (used by all further templates):
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
Or you could write a redirect view like this answer by wim with some little modification:
from django.views.generic.base import RedirectView
from django.conf.urls.static import static
re_path(r'^favicon\.ico$', RedirectView.as_view(url=static('favicon.ico'), permanent=True))
I have a django app that uses Whitenoise (hosted on Heroku) and serves my favicon from a separate folder from my static files.
Make a folder root_files at path BASE_DIR/root_files.
In settings.py:
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'root_files')
For a real-life code example checkout Mozilla's Bedrock repo. They have favicons in BASE/root_files and configure WHITENOISE_ROOT in settings.py

Problem with static files configurations - don't understand project/app staticfiles organization

I have started to develop a Django project with currently 3 apps (registration, monitor and randomization)
I wand to set parameters for statics files.
I am using bootstrap 4.0 and I have currently only one css files for sticky footer
as this css is for all template of the project, I have created a static folder at the project's root:
-project
- settings.py
- monitor
- randomization
- registration
- static
- css
- styles.css
- images
and in my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
STATIC_ROOT = 'D:/Users/jl3/DevSpace/intenseTBM_eTool/static/'
I have run the commande collectatic but I have an error:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
how can I set 'global' static files for my project?
how should I set parameters?
Well the error is pretty clear.
Also, if the STATIC_ROOT is the same as in STATICFILES_DIRS, there is no need to set it in your settings. STATICFILES_DIRS is the list of folders where Django will search for additional static files, in addition to each static folder of each app installed. STATIC_ROOT is the folder where every static files will be stored after a manage.py collectstatic.

Django any installed app not finding static files

latest versions of Django and Python, MacOS, PyCharm
I am having trouble installing any app. Everytime I try, it does not find the resources. Currently, I'm attempting to install uncaught in my Django app.
My app's name is RealEstate.
Deployment Directory for static: project_name/app_name/static/
Directory I use to copy new or revised files to Deployment Directory with collectstatic:
project_name/app_name/static_changes/
inside static, I have an admin folder with different subfolders:
static/admin/css, static/admin/fonts, static/admin/img, static/admin/js, static/admin/node_modules
settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = TRUE
STATIC_ROOT = os.path.join(BASE_DIR, 'RealEstate/static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "RealEstate/static_changes/"),
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
I have run the install for uncaught, which created a package-lock.json file and a node_modules folder with the uncaught resources, both put into the root of my project:
$ npm install --save uncaught
Since I use static_changes to copy any new or modified resources into the deployment directory static, I have copied the node_modules folder put into the project root to my app_name/static_changes/admin folder and run collectstatic
python3.7 manage.py collectstatic
It copies the files to app_name/static. But when I open the browser, the files are never found, and I get this message in the Run Console:
Not Found: /admin/node_modules/uncaught/lib/index.js
[26/Feb/2019 22:57:47] "GET /admin/node_modules/uncaught/lib/index.js
HTTP/1.1" 404 14044
I have tried moving admin/node_modules to every part of my directory structure and run collectstatic to it, and the app still never finds the resources.
Please help. Why is it that any app I try to install, it cannot see the resources, even though they exist in project_name/app_name/static/admin and I used collectstatic to get it there?
make sure that the URL in base.html is correct, like this:
<script src="{% static "admin/node_modules/uncaught/lib/index.js" %}"></script>
In macOS, I followed these steps:
https://docs.djangoproject.com/en/3.1/howto/static-files/
And ended like this:
settings.py
BASE_DIR = Path(__file__).resolve().parent.parent ---> Didn't touch this specific line
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
main.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static '/css/main.css' %}">

Django development cannot find third party script in node_modules

I'm working in a development environment with debug = True on Windows 10.
I have installed node and npm and run npm microm from the main folder of my project. It created a node_modules subfolder with a microm folder inside, among others. so the microm.js script is located in:
f:\roomtemp\node_modules\microm\microm.js
The relevant portion of my Settings file is:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'node_modules')
My template contains the following:
{% load staticfiles %}
<script src='{% static "/microm/microm.js" %}'></script>
When I attempt to load the page, the Chrome console shows a 404 and I see this in the server console:
"GET /static/microm/microm.js HTTP/1.1" 404 1661
I have read many SO questions that seem to contain the solution but none have worked so far. I've also searched high and wide on the internet. No luck so far. Any help would be much appreciated.
Mike
First of all I don't think you should STATIC_ROOT at your node_modules folder. STATIC_ROOT is where the django management commant collectstatic will put all the static files from all the apps into when you are readying for deploying. This process may result in some files being overwritten.
Please restore STATIC_URL to some sane value like
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
Then use the STATICFILES_DIRS setting. This is what tells django what extra locations to look for static files.
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'node_modules')]
Finally, make sure that your STATICFILES_FINDERS has these
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',