Reputation: 5793
I cannot get my Django app to pick up static files in production environment.
Within settings.py I've specified
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = '/var/www/static/'
and I've included django.contrib.staticfiles
in INSTALLED_APPS.
I've run manage.py collectstatic
and it is moving the static files to the /var/www/static/.
The html resulting shows, for example <link rel="stylesheet" type="text/css" href="/static/myStyleSheet.css">
from a django file which had <link rel="stylesheet" type="text/css" href="{% static 'myStyleSheet.css' %}">
.
It seems to me it should work but I'm clearly missing something. I'm getting 404 errors on the static files.
Upvotes: 6
Views: 1807
Reputation: 11070
The django server that we use locally, by running python manage.py runserver
is a development server and cannot be used in production.
In production we will use a webserver, that gets the request from HTTP/HTTPS port, and based on our configuration, forward the request to the port in which the app server (Eg. gunicorn) runs, which replaces the development server that we use.
The problem here is, the app server does not render static files by default. So, you have to configure the web server to take care of it.
In your web server configuration, you have to point the url /static/
to the static root folder. Thus, the web server will render the static files from the folder that it points to
Edit
Since you are using apache, add this to the apache config to get static files to work
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
So, here we are aliasing /static to the path to the static root. Now, a request for /static/css/my.css will be changed as: /home/user/myproject/static/css/my.css
and the corresponding file will be sent back
Upvotes: 7