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')]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34702>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
I think you've misunderstood the expected behavior. From the
`STATICFILES_DIRS` [https://docs.djangoproject.com/en/dev/ref/settings
/#std-setting-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.
--
Ticket URL: <https://code.djangoproject.com/ticket/34702#comment:1>