I’ve created a style sheet in mysite/polls/static/polls/style.css
And a link in polls/templates/polls/index.html to that as suggested by the tutorial.
But the CSS changes are not taking effect.
I’m serving this site with Apache using mod_wsgi.
Could there be some missing configuration in Apache to allow the static files to load?
Welcome @jonathanreznik1 !
Are you seeing the requests being issued for that file? (In the apache log) Are those requests successful? Or are you getting some kind of error? (If so, please post the details of the error.)
There are a couple of situations where your browser could be cacheing that file and not retrieving a changed version.
One of the easiest ways to check for this is to try accessing the page using “private” or “incognito” mode.
You can also look at the network tab in your browser’s developer tools to verify whether a request is being made or if the file is being served by the browser’s cache.
Thanks for the quick response KenWhitesell.
The apache log is showing a 404 error for the file /static/polls/style.css
192.168.2.12 - - [08/Aug/2024:19:58:33 -0700] “GET / HTTP/1.1” 304 -
192.168.2.12 - - [08/Aug/2024:20:00:10 -0700] “GET / HTTP/1.1” 404 2185
192.168.2.12 - - [08/Aug/2024:20:00:14 -0700] “GET /polls/ HTTP/1.1” 200 420
192.168.2.12 - - [08/Aug/2024:20:00:14 -0700] “GET /static/polls/style.css HTTP/1.1” 404 2269
I know the files are setup correctly in django because when I run the the django builtin server the .css loads properly. I don’t think it is a browser issue because I have tried from several computers to access it and get the same thing each time - no CSS. Why can’t Apache find that file?
What is your Apache configuration for the /static/
url location?
What is your STATIC_ROOT
setting? Is STATIC_ROOT
set to a directory that Apache will have access to?
Did you run collectstatic
?
See the docs for Deployment with managing static files for more information.
Note: What runserver does regarding static files really is irrelevant when it come to operating in a deployment environment. How static files are handled are completely different, and what happens in development has nothing to do with deployment.
Ok, so I think I originally set STATIC__URL = ‘static/’
and STATIC_ROOT = os.path.join(BASE_DIR, “static/”)
But I don’t think I ran collectstatic. How often is that needed? It didn’t mention it in the tutorial I don’t think.
As for my Apache Configuration file, there are several directories with “Require all granted” permissions, but I don’t see one for the poll/static file. I thought because of namespacing of the static directory inside polls that it would not require more permissions though. I’m definitely confused.
Here is the portion of the Apache config:
<VirtualHost *:8080>
# Other directives here
Alias /robots.txt /var/www/myDjangoProj/static/robots.txt
Alias /ascii-art/ /var/www/myDjangoProj/static/ascii-art/
#Alias /static /var/www/myDjangoProj/static
<Directory /var/www/myDjangoProj/static>
Require all granted
</Directory>
<Directory /var/www/myDjangoProj/static/ascii-art>
Require all granted
</Directory>
<Directory /var/www/myDjangoProj/myDjangoProj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myDjangoProj python-path=/var/www/myDjangoProj
WSGIProcessGroup myDjangoProj
WSGIScriptAlias / /var/www/myDjangoProj/myDjangoProj/wsgi.py
</VirtualHost>
Do you want to see my entire Apache config?
Currently there are some other files I have in the myDjanoProj/static folder. Do I need to remove them?
I ran collectstatic and it’s working now! Thank you for showing me the importance of the difference between development and deployment
Generally speaking, not a good idea. Your STATIC_ROOT should be an “Apache-friendly” directory, such as something under '/var/www/`. (See the docs referenced above.)
Every time you deploy.
The Django tutorial doesn’t cover deployment. The important docs for you to read would be at:
- How to deploy Django | Django documentation | Django
- Deployment checklist | Django documentation | Django
and - How to manage static files (e.g. images, JavaScript, CSS) | Django documentation | Django
This is a different issue than what I was addressing above. The Apache process itself needs “file read” permissions on all the static files, and “execute” permission on the directories. Keeping your static files within the directory structure of your project means you would need to grant access to that directory to Apache, which on general principles is not a good idea.
The Serving files section of the deployent docs covers this in more detail.