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.
Serving static files in production. The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory ( STATIC_ROOT ) to be moved to the static file server and served.
To make Django development server serve static we have to add a URL pattern in sitewide urls.py file. Now visit http://127.0.0.1:8000/media/python.png again, this time you should be able to see the image. Just as with static files, in the production, you should always use a real web server to serve media files.
Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.
The local Django server will serve static files but a production server such as Gunicorn or uWSGI will not. Therefore, Django comes with the built-in collecstatic command that compiles all static files into a single directory suitable for deployment into production.
There are two main ways to structure templates in a Django project as outlined in this tutorial. Let's assume we are using a templates/base.html file for a Blog project. To add our static base.css file to it we'd start by adding {% load static %} at the top of the file and then use the {% static %} tag with the path to it.
Static and media files are sometimes referred to as static and media assets. The Django admin comes with some static files, which are stored in version control on GitHub. Adding to the confusion between static and media files is that the Django documentation itself doesn't do a great job differentiating between the two.
When the collectstatic command is run, Django uses storage classes to determine how the static files are stored and accessed. Again, this is configured via the STATICFILES_STORAGE setting.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With