static file respond 404Error on tutorial

Hello everyone. I’m beginner of Django. Now I’m trying to finish tutorial. I got trouble on step6.
Google chrome developer tool’s console says

‘localhost/:1 Refused to apply style from ‘http://localhost:8080/static/practice1/style.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.’

I’m testing it on my virtualmachine using Vagrant. About detail enviroment please check github repository below.

I’m following django tutorial steps. I changed only 3 things intentionaly.

1.Changed app name from polls to practice1.
2.Changed setting to use postgres server.
3.Changed setting to use apache and wsgimod.

In my understanding way toplace my static files is following steps.
1.Check setting.py parameter ‘DEBUG = True’.
2 Set parameter ‘STATIC_URL’ & ‘STATIC_ROOT’ in setting.py.
3.Place static files on /PROJECT_ROOT/APP_ROOT/static/APP_NAME_FOLDER/STATCI_FILE_SELF (ex./my_first_django/polls/static/polls/style.css)

I know threre is some similer topix here. As following



I tried “python manage.py collectstatic”.(even it looks like used on produciton mode)
I tried adding following code on setting.py.
“import mimetypes
mimetypes.add_type(“text/css”, “.css”, True)”
I tried set “STATICFILES_DIRS” in setting.py like “PROJECT_ROOT/polls/static/polls/style.css”.(I think this is setting for when I want to place file in another place where is not in app directory.)
But couldnt solve this error.

Plese help me solve this error. And let me know whatis wrong with me.

What happens if you point your browser directly at:
http://localhost:8080/static/practice1/style.css?

You should get the actual css file, in which case the problem is probably related to the mimetypes issue.

However, if it returns a 404 in this situation, then it is a static-file related setting.

Thank you for respose.
Here is the response of http://localhost:8080/static/practice1/style.css

"Page not found (404)
Request Method: GET
Request URL: http://localhost:8080/static/practice1/style.css
Using the URLconf defined in djangopractice.urls, Django tried these URL patterns, in this order:

admin/
practice1/
The current path, static/practice1/style.css, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page."

It looks like static-file setting issue.

How exactly are you running your environment? Are you doing this with a runserver command? Or are you running this under a real web server such as Apache / mod_wsgi or nginx / uwsgi?

If you’re currently running this is a development mode, then you wouldn’t need to use collectstatic. The runserver command will load static files from within the project when DEBUG = True.

In your settings file in your repo, I didn’t see settings for STATIC_ROOT (only needed in a production mode) or STATICFILES_DIRS.

Sorry for the counfution about setting.py.

How exactly are you running your environment? Are you doing this with a runserver command? Or are you running this under a real web server such as Apache / mod_wsgi or nginx / uwsgi?
I’m runnning this envinronment with Apache / mod_wsgi.So I’m typing “systemctl restart httpd” to restart my webapp.
About django runnning mode, I’m using setting.py with “DEBUG = True”.

In your settings file in your repo, I didn’t see settings for STATIC_ROOT (only needed in a production mode) or STATICFILES_DIRS.
I tried it once but it didn’t worked. So I erased it in commiting files.
I thought it does’t needed when running as debug mode. Or do I need it when I run with Apache / mod_wsgi?

By the way, Is that possible this error caused by my httpd.conf?
I’m not familier with wsgi, so now I’m doubting it.

I added following option to my httpd.conf from default.

“”"
LoadModule wsgi_module modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGIScriptAlias / /usr/src/app/djangopractice/djangopractice/wsgi.py
WSGIPythonPath /usr/src/app/djangopractice/

<Directory /usr/src/app/djangopractice/djangopractice>

Require all granted


“”"

I’m not very sure about these settings.Should I add all static folders “Require all granted” option?
When I added those it says “does not match any patterns in djangopractice.urls”.

Ok, so you’re operating in a production-style mode.

You’ll want to start from https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi/ going through this very carefully.

I’ll also recommend https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ and https://docs.djangoproject.com/en/3.0/howto/static-files/deployment/#deploying-static-files

But very briefly -

  • Yes, collectstatic should be part of your process
  • The files should be collected to a location outside your project directory
  • You will need to set up an Apache configuration section for ‘/static’

Oh, OK.So it means, I misunderstood about differcence between debug and puroduction mode.

Thank you very much. Quick response and goood advice!
I will try it as production-style. :grinning: