Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post Django static files in production

Tags:

python

django

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.

like image 871
HenryM Avatar asked Mar 16 '16 11:03

HenryM


People also ask

How do you serve static files in Django in production?

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.

How do I serve media files in Django?

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.

How does Django find static 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.

How do I deploy a static file in Django?

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.

How do I add a static base to a Django template?

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.

What is the difference between static and media files in Django?

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.

How does the collectstatic command work in Django?

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.


1 Answers

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

like image 182
Aswin Murugesh Avatar answered Sep 30 '22 17:09

Aswin Murugesh