Trouble debugging static files not working with the local server during development

This seems like it should be a simple question but I’m not making progress and TBH I’ve always been a bit vague on the static file settings even when I got them to work alright on other projects.

My issue is fairly simple: when doing ./manage.py runserver localhost:8080 none of the files in my static directory are being served, which is bringing things to a halt. This is a new project using Django 4.0.3.

Starting with urls, in the project module urls after defining the module level imports I added

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

so I would only be serving static content via the app in production. I’ve placed a sample image in my_django_project_dir/static. When I use {% static 'test3.svg' %} in a template it renders as /static/test3.svg however the link is broken and when I try to navigate to http://localhost:8080/static/test3.svg I get a Page not found (404) error from the dev server. So I think it’s reasonably clear that the problem is we are not serving static files in local development, just not why this is happening.

Everything even kinda related to static files in my settings.py file:


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [
    "localhost", "127.7.0.1",
    "0.prod.webgrinder.uw.r.appspot.com",
    "webgrinder.uw.r.appspot.com",
    "webgrinder.app",
]


# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'tsumego',

    'django_extensions',
    'bootstrap5',
]


MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# ...

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
#STATIC_URL = os.getenv('STATIC_URL', '/static/')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
print(f"{STATIC_ROOT=}")
# This confirms that STATIC_ROOT is a fully qualified path to the static subdirectory I created within my project

That’s it, we’re already out of useful stuff in settings. If there is an obvious culprit that would be great, but failing that how do I even go about debugging this? I’ve read the django docs on static files very carefully, whatever I’m missing is not for lack of reading them at least :frowning: . Could really use some help.

FWIW the project deploys on GCP with app engine, eventually I would like to serve from a public bucket there but first we need things to work locally.

Hi bixbyr,

I’m not sure what it is exactly, but here are some things to follow-up on.

  1. Does the admin have the static files rendered (if the admin is styled, then that works)? If that’s working, install the django debug toolbar and inspect the static files for the views that don’t work.
  2. Double/triple check STATIC_ROOT and the path to your test static file.
  3. Do you have any url route handlers that are capturing the static requests before the static urlpatterns?
  4. Confirm that STATIC_URL is actually '/static/', the static() call won’t work if that value is a fully qualified URL.