Home > Net >  Serving static files after deployment with django 2.2
Serving static files after deployment with django 2.2

Time:10-28

I deployed my site at the level of my host, but the imgaes are not displayed. I did python manage.py collectstatic and it copied the files to my STATIC_ROOT

myprojet/settings.py

STATICFILES_DIRS = [
   os.path.join(BASE_DIR,'static')
]
STATIC_URL = '/static/'
STATIC_ROOT='/home/www/mySite.com/static/'

index.html

{% extends 'layout/base.html' %}
{% load staticfiles %}

{% block content %}

    <img src="{% static 'images/icone.png' %}" style="width:350px;height:100">

{% endblock content %}

I used {% load staticfiles %} as i am using django 2.2 but the images are not showing I don't know where the problem is.

CodePudding user response:

You need to configure your web server to serve the static files from the STATIC_ROOT directory. Django does not handle static files by itself in production, it only does so in development with the runserver command. Depending on your web server, you need to add some rules to tell it where to find the static files and how to serve them. For example, if you are using Apache, you can add something like this to your configuration file:

Alias /static/ /home/www/mySite.com/static/
<Directory /home/www/mySite.com/static>
Require all granted
</Directory>

This tells Apache to serve any request that starts with /static/ from the /home/www/mySite.com/static/ directory, and to allow access to it. You may need to restart or reload your web server for the changes to take effect.

Explanation:

Static files are files that do not change dynamically, such as images, CSS, JavaScript, etc. They are usually stored in a separate directory from the rest of the Django project, and they need to be collected and copied to a single location (the STATIC_ROOT) when deploying the project. This makes it easier to manage and optimize them, and to serve them efficiently with a web server.

Django provides some tools and settings to help with static files, such as the static template tag, the STATIC_URL, the STATICFILES_DIRS, and the collectstatic command. However, Django does not serve static files by itself in production, because it is not designed to be a web server and it would be inefficient and insecure to do so. Instead, it delegates the responsibility of serving static files to a web server, such as Apache, Nginx, etc. The web server is faster and more reliable at handling static files, and it can also apply some features such as compression, caching, etc.

To configure your web server to serve static files, you need to tell it where to find them and how to serve them. This depends on the web server you are using, and you may need to consult its documentation for the specific syntax and options. However, the general idea is to create an alias or a location that maps a URL path (such as /static/) to a directory on the file system (such as /home/www/mySite.com/static/). Then, you need to grant access and permissions to that directory, and optionally apply some settings to optimize the performance and security of serving static files.

Examples:

Here are some examples of how to configure different web servers to serve static files from Django:

  • Related