#34702 closed Bug (invalid)

Runserver serving static files from STATICFILES_DIRS instead of STATIC_ROOT

Reported by: Ryan Moscoe Owned by: nobody
Component: contrib.staticfiles Version: 4.2
Severity: Normal Keywords: runserver, staticfiles
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is my first Django project, so I apologize if this turns out not to be a bug--but I've done a lot of searching on Google, in the Django docs, and in the FAQ, and I am confident this is a bug. When I run collectstatic, my static files are correctly copied from the STATICFILES_DIRS directory to the STATIC_ROOT directory. However, when I run runserver, the static files are served from the STATICFILES_DIRS directory.

I am attempting to serve a React front end with a Django back end, so I have run the build on the client side to generate static files for Django to serve. However, I was getting MIME Type errors for my CSS and JS files, despite getting a status code of 200 for them. In an attempt to resolve this issue, I changed the href and src attributes for the <link> and <script> tags in my index.html file in the STATIC_ROOT directory. However, I noticed that runserver was still looking for the CSS and JS files in the same place, which made me suspicious.

With runserver still running, I started altering the href and src attributes for my <link> and <script> tags in my STATICFILES_DIRS directory (client/dist), and I saw the changes take effect in real time. The relevent settings in my settings.py file appear below:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'server.apps.ServerConfig',
    'rest_framework',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'client', 'dist')]

Change History (1)

comment:1 by Tim Graham, 12 months ago

Resolution: invalid
Status: newclosed

I think you've misunderstood the expected behavior. From the STATICFILES_DIRS documentation: "This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view."

In the future, it would be best to ask on the support channels (TicketClosingReasons/UseSupportChannels) to confirm the bug, or at least examine the code and point out what you think is at fault. Thanks.

Note: See TracTickets for help on using tickets.
Back to Top