I get the following “MIME type (‘text/html’) is not a supported” error for .css static files, nav_sidebar.js from the admin site gives:
ERR_ABORTED 404 (Not Found)
This is my first time trying to set up a production environment. Below is the setup and file contents:
I split settings files into local.py, pro.py and base.py
In base.py I have:
BASE_DIR = Path(__file__).resolve().parent.parent.parent
STATIC_URL = "/static/"
STATIC_ROOT = str(BASE_DIR / "staticfiles")
STATICFILES_DIRS = [str(BASE_DIR / "static")]
The .css loads fine for the development environment when I run:
python3 manage.py runserver --settings=mysite.settings.local
For production I run:
uwsgi --ini config/uwsgi.ini
My uwsgi.ini file contains:
[uwsgi]
module = mysite.wsgi:application
env = DJANGO_SETTINGS_MODULE=mysite.settings.pro
master = true
pidfile = /tmp/project-master.pid
http = 127.0.0.1:8000
uid = 1000
virtualenv = /home/pi/.virtualenvs/mysite/
socket = /tmp/mysite.sock
chmod-socket = 666
pythonpath = /home/pi/projects/mysite/mysite
chdir = /home/pi/projects/mysite/mysite
When I refresh the browser, the terminal shows:
[pid: 14751|app: 0|req: 6/6] 127.0.0.1 () {42 vars in 743 bytes} [Tue Nov 9 09:32:40 2021] GET /static/css/base.css => generated 179 bytes in 10 msecs (HTTP/1.1 404) 5 headers in 158 bytes (1 switches on core 0)
The admin site shows in terminal with:
[pid: 15705|app: 0|req: 9/9] 127.0.0.1 () {38 vars in 722 bytes} [Tue Nov 9 09:48:13 2021] GET /static/admin/js/nav_sidebar.js => generated 179 bytes in 10 msecs (HTTP/1.1 404) 5 headers in 158 bytes (1 switches on core 0)
I edited the /etc/hosts file to include the sample domain:
127.0.0.1 mysite(dot)com www.mysite(dot)com
The site loads at htt(p)://mysite(dot)com:8000/ but the Chromium console gives an error:
Refused to apply style from ‘(http)://mysite(dot)com:8000/static/css/base.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.
I run collectstatic:
python3 manage.py collectstatic --settings=mysite.settings.pro
Then manually move the static files from /staticfiles to /static in the root project folder.
I added this fix from a similar forum post about MIME type, into base.py with no effect:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
The direct path to (http)://mysite(dot)com:8000/static/css/base.css results in:
Not Found The requested resource was not found on this server.
In my project folder I have config/nginx.conf which contains:
location /static/ {
alias /home/pi/projects/mysite/mysite/static/;
}
My base.html template has:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}Base Template{% endblock title %}</title>
<link href="{% static 'css/base.css' %}" rel="stylesheet">
</head>
I’m using a Raspberry Pi 3B with standard Raspbian Debian (Buster), Chromium browser, Django 3.2.9, NGINX and uWSGI.
The project is a hello world style app wtih nginx and uwsgi installed via pip:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.apps.MyappConfig',
]
Any suggestions to get the static files working? Thanks. The main reason I’m trying to get production working is to continue integrating services which require https which I’m unable to get in the development environment and in the meantime I can’t load static files. I edited the links due to the post link restrictions.