Django static files settings
In my app i combine Django and FastAPI.
So I start my django server this way:
from fastapi import FastAPI
from django.core.asgi import get_asgi_application
app = FastAPI()
app.mount("/dj", get_asgi_application())
But when i open Django Admin panel i get many errors like:
"GET /dj/static/admin/css/dashboard.css HTTP/1.1" 404 Not Found.
How should i configure Django static files to get proper Admin panel?
The issue is that Django’s runserver does some "magic" behind the scenes to serve static files automatically, but Uvicorn (and ASGI in general) does not. When you mount Django inside FastAPI, you're responsible for telling the server how to find those CSS and JS files.
Since you've mounted Django at /dj, your admin panel is looking for files at /dj/static/, but currently, nothing is "listening" at that path to hand the files over.
The most robust way to fix this is using WhiteNoise. It allows your web app to serve its own static files without needing Nginx or Apache during development.
Install WhiteNoise
pip install whitenoiseUpdate settings.py Add the WhiteNoise middleware to the top of your list (immediately after SecurityMiddleware):
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # <--- Add this # ... rest of your middleware ] # Ensure you have a STATIC_ROOT defined (where files will be collected) import os from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent STATIC_URL = 'static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')Collect the files Before running your script, run this command in your terminal to gather all the admin CSS/JS files into that staticfiles folder:
python manage.py collectstatic